Jump to content

Running a python script - shebang and environment


Recommended Posts

Hi there

 

I would like to run a python script with an Alfred workflow but I realized that the python version I run in the terminal is not the same Alfred is running

The python I use was installed with homebrew, and my path is set up so I run the "brew installed" python which is 2.7.3 and is located here: 

$ which python

$ /usr/local/bin/python

 

I setup my script to print out the python version with this statement: 

$ print(sys.version)

 

When I run it in the terminal I get 2.7.3, but the workflow notification prints out 2.7.2

 

The shebang in my script.py is #!/usr/bin/env python 

 

If I set my shebang to #!/usr/local/bin/python everything work fine and the workflow notification prints out 2.7.3

But I don't want to change all my scripts and hardcode the path, I would prefer to use #!/usr/bin/env python 

 

Anybody has a trick?

Is there anything to do with my ~/.bash* dotfiles, or is this Alfred not loading the user environment?

 

Thanks for your help!

Link to comment

Your using homebrew so you have set /usr/local/bin....sbin to the beginning of your path right? If not do that and you won't have a problem. | Brew doesn't complain when you run "brew doctor" ?

 

Your right to use #!/usr/bin/env as it will pick up whatever is the first instance of python in your path. So if you go into terminal, `echo $PATH` you will see what python is getting picked up first and it must be your system install /usr/bin/ or else you would no have this problem but if you have /usr/local/bin/.... first then I have no idea. I do know that it's what directory gets read first and that is explained in this post.

 

I would only make the change to your bashrc/bash_profile or whatever file you use for your paths. You can go extreme and change /etc/paths/ to have /usr/local/ come first but I don't think that is necessary

 

Read this post for a detailed rundown of how your system figures out what and which to run... their a pecking order pretty neatly laid out. 

http://stackoverflow.com/questions/11272699/clamxav-homebrew-mac-os-x/11278352#11278352

Edited by twinpeaks
Link to comment

yep I think it is related to the PATH

 

if I run echo $PATH in the terminal i get:

/usr/local/bin:/usr/local/share/python:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

 

if I run echo $PATH in the workflow I get:

/usr/bin:/bin:/usr/sbin:/sbin

 

my ~/.bash_profile loads my ~/.bashrc which loads a ~/.path file in which I set:

 

export PATH=/usr/local/share/python:$PATH
export PATH=/usr/local/bin:$PATH
 

And, appart for some dylibs, brew doctor is happy

 

Could it be that Alfred does not load the ~/.bash_profile and so the ~/.path is not loaded in the end?

Link to comment

yep I think it is related to the PATH

 

if I run echo $PATH in the terminal i get:

/usr/local/bin:/usr/local/share/python:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

 

if I run echo $PATH in the workflow I get:

/usr/bin:/bin:/usr/sbin:/sbin

 

my ~/.bash_profile loads my ~/.bashrc which loads a ~/.path file in which I set:

 

export PATH=/usr/local/share/python:$PATH
export PATH=/usr/local/bin:$PATH
 

And, appart for some dylibs, brew doctor is happy

 

Could it be that Alfred does not load the ~/.bash_profile and so the ~/.path is not loaded in the end?

 

Alfred, indeed, does not load your local profile. Try providing the full path to the python executable that you wish to use when executing the script

Link to comment

I'd rather use #!/usr/bin/env python so my script is portable to other installations

 

I sourced my .bash_profile before calling for my python script and now it works 

the run script node looks like this:

 

source ~/.bash_profile
~/tools/myscript.py {query}

I am happy with this solution for now, and this closes this topic.

 

But I keep thinking Alfred should run a session in the user environment, like any other terminal, iterm and alike 

I think this would be more elegant and straightforward 

This could be a new discussion of its own, I don't know all the consequences of such a change, so I'll leave it up to you guys

 

 

Thanks a lot for your help solving this issue.

Cheers!

 

Link to comment

I'd rather use #!/usr/bin/env python so my script is portable to other installations

 

I sourced my .bash_profile before calling for my python script and now it works 

the run script node looks like this:

 

source ~/.bash_profile
~/tools/myscript.py {query}

I am happy with this solution for now, and this closes this topic.

 

But I keep thinking Alfred should run a session in the user environment, like any other terminal, iterm and alike 

I think this would be more elegant and straightforward 

This could be a new discussion of its own, I don't know all the consequences of such a change, so I'll leave it up to you guys

 

 

Thanks a lot for your help solving this issue.

Cheers!

 

 

Glad you were able to solve your issue. Let me know if there is anything else that we can assist you with

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...