Python Language Virtual environments Specifying specific python version to use in script on Unix/Linux


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 #!:


If you are in a virtual environment, then python will use the Python from your virtual environment, but ./ 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

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