0

while there are multiple versions of Python installed on a Linux machine, is there a way to mention in the script to be open with a specific version, say, 3.8 even if we issue the #python script.py as opposed to python3.8 script.py ? I don't want to use Linux alias command. I wanna know if that is possible to be accomplished within the script

brucelona
  • 1
  • 1
  • You could write your own wrapper (e.g. a shell script) that takes a version option and then dispatches to the specified Python version. – Tom Karzes Feb 14 '20 at 06:24
  • 1
    Does this answer your question? [Multiple Python versions on the same machine?](https://stackoverflow.com/questions/2547554/multiple-python-versions-on-the-same-machine) – chimbu Feb 14 '20 at 06:31

2 Answers2

1

Use shebang. #!/usr/bin/python
In the first line of the code with the serial number of Python you want at the end.

You'll need to then run your script like ./my_script.py

spinyBabbler
  • 392
  • 5
  • 19
0

You can select the interpreter by adding #!/usr/bin/env pythonXX as the first line in your script, provided the version is in the path. You can also invoke the interpreter directly with the script as the argument.

pythonXX script.py

Depending on your situation

pythonXX -E script.py

might be better. That way the interpreter ignores paths set by environmental variables. For more details view https://docs.python.org/3/using/cmdline.html

Michael
  • 104
  • 4