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 (OS X) 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
which python
Output:
/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 (from within 'venv_name'), I got an error:
./script.py
Output:
./script.py: line 2: import: command not found
./script.py: line 3: print: command not found
Although running that path directly on the command line (from within 'venv_name') works fine:
/Users/username/.pyenv/shims/python script.py
Output:
success
And:
python script.py # Also works
Output:
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.
#!/home/dslima90/.virtualenvs/production_enviroment/bin/python
, maybe check your path? Aren't you missing bin? – Hame#! python
? – Hame#! python
and that gives the error-bash: ./script.py: python: bad interpreter: No such file or directory
– Unwrap#!/usr/bin/env python
should do it for you... You can try on the command line before. See if it is calling the right interpreter. – Hameod -c script.py
if the#!
really are the first two characters. – Bullfrog#!
are indeed the first 2 chars:0000000 # ! / U s
– Unwrap.../shims/python
another script? – Bullfrog... exec $pyenv_python
at the end. That file is created bypyenv
and I just assumed it make a copy or link of a python executable, but that it is not the case. mystery solved! – Unwrap