How to Tell a Python Script to Use a Particular Version

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

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.

how to specify version of python to run a script?

Set the shebang (script header) to the path to python3.4 which you can get using which.

For example, here's what do I have:

$ which python
/usr/bin/python
$ which python3.4
/usr/local/bin/python3.4

Then, just set the shebang appropriately:

#!/usr/local/bin/python3.4

How to invoke a specific Python version WITHIN a script.py -- Windows

If you have installed Python 3.3 on your system it added a new launcher for Python scripts which means you can use a shebang line:

#!python2.7
print "Hello"

or

#!python3.3
print("World")

will both work and run the appropriate python, or you can specify a full path to a Python interpreter or create an ini file that defines abbreviations for specific Python interpreters.

See PEP 397 for the different options available in Windows shebang lines.

If you don't want to install Python 3.3 then you can also install the launcher separately.

How to make sure the script is executed by a specific version of python?

Just add this to the start of your script:

import sys

assert sys.version_info >= (3, 5, 2), "Python version too low."

Or if you prefer without assert:

import sys

if not sys.version_info >= (3, 5, 2):
raise EnvironmentError("Python version too low.")

Or little bit more verbosely, but this will actually inform the user what version they need:

import sys

MIN = (3, 5, 2)
if not sys.version_info >= MIN:
raise EnvironmentError(
"Python version too low, required at least {}".format('.'.join(str(n) for n in MIN)))

Example output:

Traceback (most recent call last):
File "main.py", line 6, in <module>
"Python version too low, required at least {}".format('.'.join(str(n) for n in MIN)))
OSError: Python version too low, required at least 3.5.2

 

The specified minimum version tuple can be as precise as you want. These are all valid:

MIN = (3,)
MIN = (3, 5)
MIN = (3, 5, 2)

How do I check which version of Python is running my script?

This information is available in the sys.version string in the sys module:

>>> import sys

Human readable:

>>> print(sys.version)  # parentheses necessary in python 3.       
2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)]

For further processing, use sys.version_info or sys.hexversion:

>>> sys.version_info
(2, 5, 2, 'final', 0)
# or
>>> sys.hexversion
34014192

To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code:

assert sys.version_info >= (2, 5)

This compares major and minor version information. Add micro (=0, 1, etc) and even releaselevel (='alpha','final', etc) to the tuple as you like. Note however, that it is almost always better to "duck" check if a certain feature is there, and if not, workaround (or bail out). Sometimes features go away in newer releases, being replaced by others.

How do I detect the Python version at runtime?

Sure, take a look at sys.version and sys.version_info.

For example, to check that you are running Python 3.x, use

import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")

Here, sys.version_info[0] is the major version number. sys.version_info[1] would give you the minor version number.

In Python 2.7 and later, the components of sys.version_info can also be accessed by name, so the major version number is sys.version_info.major.

See also How can I check for Python version in a program that uses new language features?

Testing a python script in a specific version

You could just install a Python 2.5.2.

I have 3 different versions Python installed on my Lucid and they use different links under /bin/ so it's easy to call the specific version

python -> python3 ->python3.1

python2 -> python2.7

python2.5

How can I check for Python version in a program that uses new language features?

You can test using eval:

try:
eval("1 if True else 2")
except SyntaxError:
# doesn't have ternary

Also, with is available in Python 2.5, just add from __future__ import with_statement.

EDIT: to get control early enough, you could split it into different .py files and check compatibility in the main file before importing (e.g. in __init__.py in a package):

# __init__.py

# Check compatibility
try:
eval("1 if True else 2")
except SyntaxError:
raise ImportError("requires ternary support")

# import from another module
from impl import *

How to detect Python Version 2 or 3 in script?

sys.version_info provides the version of the used Python interpreter.

Python 2

>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
>>> sys.version_info[0]
2

Python 3

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=10, releaselevel='final', serial=0)
>>> sys.version_info[0]
3

For details see the documentation.



Related Topics



Leave a reply



Submit