Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an executable python script run inside the current virtual environment?

So what I'm trying to do is have an executable python script so that I can link to it on my path and run it from anywhere, but I need to run it within a virtual environment locally.

Currently I have a symbolic link in /usr/local/bin -> ~/dev/project/tools/rest_client.py

Inside the project directory the permissions are:

-rwxr-xr-x  1 luke  staff  3229 Dec  3 10:21 rest_client.py 

The rest-client file

#!/Users/luke/Envs/py2.7/bin/python

def main():
   #do stuff

I can run it from any directory like this when I run rest-client

I would like to be able to check this file into a git repo and share it with others, without hardcoding the virtualenv into the file, but to still be able to execute it from anywhere on my machine.

If I change the first line to

#!/usr/bin/python

Then it wont run in the virtual environment unless I execute it via

workon py2.7
python ~/dev/project/tools/rest_client.py

Is there a proper way to do this is UNIX without using something like an alias?

like image 593
Luke Exton Avatar asked Dec 03 '14 18:12

Luke Exton


1 Answers

Put #!/usr/bin/env python as your shebang line to inherit the current python environment.

like image 145
Sam Dolan Avatar answered Oct 12 '22 03:10

Sam Dolan