How do 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 reference it in my path and run it from anywhere, but I need to run it locally in a virtual environment.

I currently have a symlink in / usr / local / bin -> ~ / dev / project / tools / rest_client.py

Inside the project directory permissions:

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

      

Rest-client file

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

def main():
   #do stuff

      

I can run it from any directory, for example at startup rest-client

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

If I change the first line to

#!/usr/bin/python

      

Then it won't run in a virtual environment unless I have executed it with

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

      

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

+3


source to share


1 answer


Place #!/usr/bin/env python

as your shebang line to inherit your current python environment.



+9


source







All Articles