Specifying specific python version to use in script on Unix/Linux

Download python eBook

Example

In order to specify which version of python the Linux shell should use the first line of Python scripts can be a shebang line, which starts with #!:

#!/usr/bin/python

If you are in a virtual environment, then python myscript.py will use the Python from your virtual environment, but ./myscript.py will use the Python interpreter in the #! line. To make sure the virtual environment's Python is used, change the first line to:

#!/usr/bin/env python

After specifying the shebang line, remember to give execute permissions to the script by doing:

chmod +x myscript.py

Doing this will allow you to execute the script by running ./myscript.py (or provide the absolute path to the script) instead of python myscript.py or python3 myscript.py.

Stats

Contributors: 4
2017-03-28
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook