Specify Python interpreter for Python script in Unix and Windows

given this script: test.py:


import sys
print sys.executable

what’s the output of these following scripts?

. venv/bin/activate
(venv) ./test.py
. venv/bin/activate
(venv) python test.py



the Unix Shebang will be used to locate the interpreter when the script triggered directly.

in the example, Shebang is hard-coded to /usr/bin/python, actually there’s a more flexiable way which uses the path to find the interpreter:

#!/usr/bin/env python

it works perfect with the virtualenv.



if you want to run your python script using a virtual environment on a Unix machine:

  • use #!/usr/bin/env python as the Shebang:
. venv/bin/activate
(venv) ./test.py
  • or use the python interpreter directly:

    /tmp/venv/bin/python test.py


There’s no Shebang for Windows, it you run test.py, it always uses the default python interpreter. if you want to specify the python interpreter, use it directly:

c:\tmp\venv\Scripts\python c:\tmp\test.py
- 2016-05-30 edit