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
.