Jump to content

Problem executing python script from shell


Recommended Posts

hey, I need help because I habe no Idea where to find my mistake:

 

I wrote a Python Script to facilitate my vocabulary learning. In the end it is a pretty easy script which takes the word as an input, runs a request, scrapes the translation and prints me this translation on the screen. I didn't want to open my browser every time.

In fact this worked pretty well until some time ago and I have no idea why it stopped working. The workflow is pretty easy to: Keyword with argument runs a scipt in my shell which start parses the input in UTF8 and then starts the srcript, the output of the scipt is printet to the sdtout, which is either the terminal or the in the case of Alfred a large type popup.

 

QUERY=$(iconv -f UTF-8-MAC <<<"{query}")
python3 -u "/Users/***/Engl.py" $QUERY

 

if I put this directly into the terminal and run in, it looks like this

 

 MacBook-Pro  ~ QUERY=$(iconv -f UTF-8-MAC <<<"häßlich")
 MacBook-Pro  ~  echo $QUERY
häßlich
 MacBook-Pro  ~ python3 -u "/Users/**/Engl.py" $QUERY
häßlich
-----
uglier adj 
unsightlier adj 
ugly adj 
unpleasant adj 
hideous adj 
nasty adj 
unsightly adj 
loathsome adj 

häßlich (comparative häßlicher, superlative am häßlichsten)
Formerly standard spelling of hässlich which was deprecated in the spelling reform (Rechtschreibreform) of 1996.

 

But when I run the same from alfred there is no output as it used to be. Stil I know that the script works because the script  also writes into a database, which works. Only the printing suddenly seem to have stopped working. I also tried to use  the
 

import sys

s = sys.stdout
for a in my_input:
s.write(a + '\n')

 

Instead of just printing in python but no change. To test the printing function I also ran an easy script from alfred which only prints "hey". This also worked so as I see it every part of the worklow works itself just not as whole.

 

Here is the part of the script which should print out the stuff

 

if __name__ == "__main__":
    if len(sys.argv) == 1:
        webbrowser.open("https://www.linguee.de/deutsch-englisch/")
    elif len(sys.argv) == 2:
        query = sys.argv[1]
        print(query + "\n" + "-----")
        abc= request_Linguee(query, buff=1)["Translation"]
        print(abc)
        defenglish = definition_english(query)
        print(defenglish)

    else:
        query = ""
        ag1 = len(sys.argv)
        for i in sys.argv[1:ag1]:
            query += i + " "
        #print (query)
        print(query + "\n" + "-----")
        print(request_Linguee(query, buff=1))
else:
    print("Nope: ")
    print("__name__")

 

I'm thankful for every idea, where there could be a problem.

 

 

Edited by Butler
Link to comment
24 minutes ago, Butler said:

QUERY=$(iconv -f UTF-8-MAC <<<"{query}")

 

This is a bit silly because Python 3 is Unicode-aware and takes care of its own encoding/decoding. You should remove it, and replace it with unicodedata.normalize('NFC', query) if necessary.

 

24 minutes ago, Butler said:

python3 -u "/Users/***/Engl.py" $QUERY

 

This is suboptimal, too.

 

The best way to run a Python 3 script is to put the script inside the workflow and use Alfred’s External script option, so you aren’t starting an extra shell process for no reason. Certainly, you should avoid using {query} in scripts. That’s a legacy mode for backwards compatibility. “with input as argv” works much better.

 

If you do need to use a shell for some reason, use "$QUERY", not $QUERY, so your script only gets one argument instead of having to stitch multiple arguments back together into the original query.

 

24 minutes ago, Butler said:

Here is the part of the script which should print out the stuff

 

I'm sorry, nobody is going to try to recreate your workflow for themselves just to help you debug it. It's a complete waste of their time and probably won't be the same as your workflow, anyway.

 

Put the Python script(s) inside the workflow itself, upload it somewhere (Dropbox?) and post a link to it, so we can see it for ourselves.

 

Edited by deanishe
Link to comment

Thanks for your comments.

 

 

14 minutes ago, deanishe said:

This is a bit silly because Python 3 is Unicode-aware and takes care of its own encoding/decoding. You should remove it, and replace it with unicodedata.normalize('NFC', query) if necessary.

 

My problem there was not python, but Alfred as far as I understand it. I had the problem that the German "Umlaute" like ä,ö,ü weren't properly transmitted to the python script, unicodedata did not solve this problem, QUERY=$(iconv -f UTF-8-MAC <<<"{query}")  did. When I used Umlaute in a query defined in the script or passed to the script from the terminal there was no problem because as you said python3 is unicode aware but somewhere in a forum somebody said that somewhere in Alfred umlaute aren't properly processed.

 

24 minutes ago, deanishe said:

he best way to run a Python 3 script is to put the script inside the workflow and use Alfred’s External script option, so you aren’t starting an extra shell process for no reason.

I  do it like that because I use the same script somewhere else and I don't want to copy all changes into the workflow every time.

27 minutes ago, deanishe said:

Certainly, you should avoid using {query} in scripts. That’s a legacy mode for backwards compatibility. “with input as argv” works much better.

I don't understand what "That’s a legacy mode for backwards compatibility." means, do you have some site where I can read about this or could you explain? But I will try it.

 

28 minutes ago, deanishe said:

If you do need to use a shell for some reason, use "$QUERY", not $QUERY, so your script only gets one argument instead of having to stitch multiple arguments back together into the original query.

fair point, I did not think about that. Thanks

 

29 minutes ago, deanishe said:

I'm sorry, nobody is going to try to recreate your workflow for themselves just to help you debug it. It's a complete waste of their time and probably won't be the same as your workflow, anyway.

Oh I did not think somebody would want to recreate it to debug it. I hoped to receive some tipps about what could it be. In fact the rest of the code is nothing special and as the code works in principle and does not throw an error in the terminal I tought it might be unnecessary.

Link to comment
Just now, Butler said:

but somewhere in a forum somebody said that somewhere in Alfred umlaute aren't properly processed.

 

That was true in Alfred 2, but it hasn't been for years.

 

3 minutes ago, Butler said:

I don't understand what "That’s a legacy mode for backwards compatibility." means

 

Alfred used to only work with the {query} text macro for input. Now it also supports ARGV, which is fundamentally better for multiple reasons.

 

5 minutes ago, Butler said:

Oh I did not think somebody would want to recreate it to debug it

 

What else are we supposed to do? You say the problem only occurs in Alfred, and we can't run code in our heads.

 

5 minutes ago, Butler said:

I hoped to receive some tipps about what could it be.

 

You're asking us to guess what you might have done wrong, basically, and that's a waste of everybody's time.

 

I know text encoding in Python extremely well. I know exactly what input Linguee requires because I've scraped it myself. I could almost certainly identify and fix the issue with your workflow (and demonstrate the other things) in under 2 minutes if I had the workflow.

 

So I'm sure you can understand that I am not going to spend an hour or more guessing at all the things that might be wrong, instead.

 

If you have any further questions, please upload the workflow somewhere so we can see what you're actually talking about.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...