jesuis
jesuis

Reputation: 2326

How do I tell a Python script to use a particular version

How do I, in the main.py module (presumably), tell Python which interpreter to use? What I mean is: if I want a particular script to use version 3 of Python to interpret the entire program, how do I do that?

Bonus: How would this affect a virtualenv? Am I right in thinking that if I create a virtualenv for my program and then tell it to use a different version of Python, then I may encounter some conflicts?

Upvotes: 112

Views: 329398

Answers (9)

.python-version or pyproject.toml

One day we'll standard a way to do this. I believe. But for now, just drop that information information in the standard version setting mechanism your your Python version selector of choice.

pyenv has the .python-version file which can be created with:

pyenv local 3.8.0

The file simply contains the string 3.8.0. Users can then enable that specific python version with:

pyenv local

In Python Poetry you can add to your pyproject.toml:

[tool.poetry.dependencies]
python = "~2.7 || ^3.2"  # Compatible python versions must be declared here

Or worse case, just say it in the README until the standard comes.

Advantage of #!/usr/bin/env python shebang without versions

Minor version shebangs like #!/usr/bin/env python3.7 are likely a bad idea, because usually your project will work on a range of Python versions, e.g. as is possible to specify on pyproject.toml. So if you specify python3.7 it won't work if someone has 3.8, even though major versions should (in theory) be backworks compatible (I have seen breaks in the past however I believe).

Using specific major version shebangs such as #!/usr/bin/env python3 has that downside that if you have 50 shebangs in your project, your are duplicating information everywhere, so an update will require all of them. It is better to keep just let your python version manager system symlink to python.

Perhaps using major versions to differentiate python2 vs python3 is not totally unreasonable, as it might allow those files to run on a few distros without modification. But ultimately part of me feels like it is somewhat inevitable for distros to produce their own patches or wrappers on python packages, as no single distro can have all packages necessarily working with their specific chosen Python version, so it is kind of elegant to just keep #!/usr/bin/env python most o the time.

Upvotes: 0

GMG
GMG

Reputation: 5

While working with different versions of Python on Windows,

I am using this method to switch between versions.

I think it is better than messing with shebangs and virtualenvs

  1. install python versions you desire

  2. go to Environment Variables > PATH

    (I assume that paths of python versions are already added to Env.Vars.>PATH)

  3. suppress the paths of all python versions you dont want to use

    (don't delete the paths, just add a suffix like "_sup")

  1. call python from terminal

    (so Windows will skip the wrong paths you changed, and will find the python.exe at the path you did not suppressed, and will use this version after on)

  2. switch between versions by playing with suffixes

Upvotes: -4

Andrew
Andrew

Reputation: 986

I would use the shebang #!/usr/bin/python (first line of code) with the serial number of Python at the end

Then run the Python file as a script, e.g., ./main.py from the command line, rather than python main.py.

It is the same when you want to run Python from a Linux command line.

Upvotes: 25

DevPlayer
DevPlayer

Reputation: 5599

While the OP may be working on a nix platform this answer could help non-nix platforms. I have not experienced the shebang approach work in Microsoft Windows.

Rephrased: The shebang line answers your question of "within my script" but I believe only for Unix-like platforms. Even though it is the Unix shell, outside the script, that actually interprets the shebang line to determine which version of Python interpreter to call. I am not sure, but I believe that solution does not solve the problem for Microsoft Windows platform users.

In the Microsoft Windows world, the simplify the way to run a specific Python version, without environment variables setup specifically for each specific version of Python installed, is just by prefixing the python.exe with the path you want to run it from, such as C:\Python25\python.exe mymodule.py or D:\Python27\python.exe mymodule.py

However you would need to consider the PYTHONPATH and other PYTHON... environment variables that would point to the wrong version of Python libraries.

For example, you might run:

C:\Python2.5.2\python.exe mymodule

Yet, the environment variables may point to the wrong version as such:

PYTHONPATH = D:\Python27

PYTHONLIB = D:\Python27\lib

Loads of horrible fun!

So a non-virtualenv way, in Windows, would be to use a batch file that sets up the environment and calls a specific Python executable via prefixing the python.exe with the path it resides in. This way has additional details you'll have to manage though; such as using command line arguments for either of the "start" or "cmd.exe" command to "save and replace the "console" environment" if you want the console to stick around after the application exits.

Your question leads me to believe you have several Python modules, each expecting a certain version of Python. This might be solvable "within" the script by having a launching module which uses the subprocess module. Instead of calling mymodule.py you would call a module that calls your module; perhaps launch_mymodule.py

launch_mymodule.py
import sys
import subprocess
if sys.argv[2] == '272':
  env272 = {
    'PYTHONPATH': 'blabla',
    'PYTHONLIB': 'blabla', }
  launch272 = subprocess.Popen('D:\\Python272\\python.exe mymodule.py', env=env272)

if sys.argv[1] == '252'
  env252 = {
    'PYTHONPATH': 'blabla',
    'PYTHONLIB': 'blabla', }
  launch252 = subprocess.Popen('C:\\Python252\\python.exe mymodule.py', env=env252)

I have not tested this.

Upvotes: 17

Chris Hanning
Chris Hanning

Reputation: 143

For those using pyenv to control their virtual environments, I have found this to work in a script:

#!/home/<user>/.pyenv/versions/<virt_name>/bin/python

DO_STUFF

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142256

You can add a shebang line the to the top of the script:

#!/usr/bin/env python2.7

But that will only work when executing as ./my_program.py.

If you execute as python my_program.py, then the whatever Python version that which python returns will be used.

In re: to virtualenv use: virtualenv -p /usr/bin/python3.2 or whatever to set it up to use that Python executable.

Upvotes: 96

Forrest Holub
Forrest Holub

Reputation: 9

I had this problem and just decided to rename one of the programs from python.exe to python2.7.exe. Now I can specify on command prompt which program to run easily without introducing any scripts or changing environmental paths. So i have two programs: python2.7 and python (the latter which is v.3.8 aka default).

Upvotes: -2

Gravity Grave
Gravity Grave

Reputation: 2862

Perhaps not exactly what you asked, but I find this to be useful to put at the start of my programs:

import sys

if sys.version_info[0] < 3:
    raise Exception("Python 3 or a more recent version is required.")

Upvotes: 69

user177800
user177800

Reputation:

You can't do this within the Python program, because the shell decides which version to use if you a shebang line.

If you aren't using a shell with a shebang line and just type python myprogram.py it uses the default version unless you decide specifically which Python version when you type pythonXXX myprogram.py which version to use.

Once your Python program is running you have already decided which Python executable to use to get the program running.

virtualenv is for segregating python versions and environments, it specifically exists to eliminate conflicts.

Upvotes: -1

Related Questions