what shebang to use for python scripts run under a pyenv virtualenv

44,132

Solution 1

I don't really know why calling the interpreter with the full path wouldn't work for you, I use it all the time, but if you want to use the python interpreter that is in your environment you should do:

#!/usr/bin/env python

That way you search your environment for the python interpreter to use.

Solution 2

As you expected, you should be able to use the full path to the virtual environment's python in the shebang to choose/control the environment the script runs in regardless of the environment of the controlling script.

In the comments on your question, VPfB & you find that the /Users/username/.pyenv/shims/python is a shell script that does an exec $pyenv_python. You should be able to echo $pyenv_python to determine the real python and use that as your shebang.

See also: https://unix.stackexchange.com/questions/209646/how-to-activate-virtualenv-when-a-python-script-starts

Try pyenv virtualenvs to find a list of virtual environment directories.

And then you might find a using shebang something like this:

#!/Users/username/.pyenv/python/versions/venv_name/bin/python
import pandas as pd
print 'success'

... will enable the script to work using the chosen virtual environment in other (virtual or not) environments:

(venv_name) $ ./script.py 
success
(venv_name) $ pyenv activate non_pandas_venv 
(non_pandas_venv) $ ./script.py
success
(non_pandas_venv) $ . deactivate
$ ./script.py
success
$

The trick is that if you call out the virtual environment's python binary specifically, python looks around that binary's path location for the supporting files and ends up using the surrounding virtual environment. (See per How does virtualenv work? )

Solution 3

If you need to use more shell than you can put in the #! shebang line, you can start the file with a simple shell script which launches Python on the same file.

#!/bin/bash
"exec" "pyenv" "exec" "python" "$0" "[email protected]"
# the rest of your Python script can be written below

Because of the quoting, Python doesn't execute the first line, and instead joins the strings together for the module docstring... which effectively ignores it.

You can see more here.

Share:
44,132
xgord
Author by

xgord

Bio-Degradable

Updated on June 15, 2022

Comments

  • xgord
    xgord less than a minute

    When a python script is supposed to be run from a pyenv virtualenv what is the correct shebang for the file?

    As an example test case, the default python on my system (OSX) does not have pandas installed. The pyenv virtualenv venv_name does. I tried getting the path of the python executable from the virtualenv.

    $ pyenv activate venv_name
    (venv_name)$ which python
    /Users/username/.pyenv/shims/python
    


    So I made my example script.py:

    #!/Users/username/.pyenv/shims/python
    import pandas as pd
    print 'success'
    


    But when I tried running the script, I got an error:

    (venv_name) $ ./script.py
    ./script.py: line 2: import: command not found
    ./script.py: line 3: print: command not found
    


    Although running that path on the command line works fine:

    (venv_name) $ /Users/username/.pyenv/shims/python script.py
    success
    
    (venv_name) $ python script.py # also works
    success
    

    What is the proper shebang for this? Ideally, I want something generic so that it will point at the python of whatever my current venv is.