Change Python execution version from within Python script

change python version
how to change python version in cmd
programmatically get python version
set python version script
how to specify python version in python file
specify version of python in script
atom change python version
change python version runtime

Is it possible to change execution version of Python within a script?

I have Python 2.6.6 which gets loaded by default. I want to change this version to 3.6.0, which is installed at custom location(not /usr/bin), inside a script. So, in the script, I will check for Python version with sys.version , will load Python 3.6.0 module in script. It is not getting reflected in the run environment. Here is the code:

import sys, os
pyVersion = int(sys.version.split(" ")[0].replace(".", ""))
exec(open(os.environ['MODULESHOME']+"/init/python.py").read())
if pyVersion < 360:
    print("Python 3.6.0 version required")
    print("Loading utils/python module")
    module(['load', 'utils/python/3.6.0'])
    module('li')

It is listing modules as expected. Output:

Python 3.6.0 version required
Loading utils/python module
Currently Loaded Modulefiles:
1) licenses                    2) cliosoft/6.32.p3(default)   3)utils/python/3.6.0

Now, when I check python version in next line, It is still python 2.6.6

print(sys.version)

Output:

2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]

Now, how can I make Python to use 3.6.0 for all the lines of code after loading module?

NOTE: I can change version before loading the script. But, it will be used by multiple users and I don't have root access. So, not possible to change version for every one. And I'm interested in doing it this way only.


Check Python version from command line / in script, If you get the version number in the script, you can not only display it with print() , but you can also switch the code to be executed depending on� Version number string: platform.python_version() A tuple of version number strings: platform.python_version_tuple() If you get the version number in the script, you can not only display it with print(), but you can also switch the code to be executed depending on the version.


Expanding on the solution suggested by Ryan. You can avoid having the entire script be a string by doing something like

import sys, os

if sys.version[0] == '2':
    with open(__file__, 'r') as f:
        _script_text = ''.join(f.readlines())
        os.execlp('python3', 'python3', '-c', _script_text)
        exit()

# Rest of your code
# | | | | | | | | |

Of course avoiding this entirely would be better, but I think this is ever so slightly less aweful to look at.

env and python scripts: controlling the python version, How can you manually test this script under different versions of Python? the primary Python interpreter installed on the user's system, at script execution time. it's Python 2.5; the first python in the default path is /usr/bin/python, which is a Temporarily editing the file, to change the argument to env, may do the trick. C:\1 λ python -V Python 3.6.1 C:\1 λ pip -V pip 9.0.1 from c:\python36\lib\site-packages (python 3.6) C:\1 λ python version.py Hello from 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]


env and python scripts: controlling the python version On Unix-like systems, python scripts will often start with this line:

#!/usr/bin/env python

How can you manually test this script under different versions of Python?

This "env-shebang" idiom effectively selects the primary Python interpreter installed on the user's system, at script execution time. On any given system, this will resolve to a particular version of Python. For instance, on stock Debian Lenny GNU/Linux, it's Python 2.5; the first python in the default path is /usr/bin/python, which is a symbolic link to /usr/bin/python2.5.

Now, if you're developing a Python script that is supposed to work on several different Python versions, and want to test it under each, the "env-shebang" idiom can get in your way. Temporarily editing the file, to change the argument to env, may do the trick. But that's error prone and difficult to automate.

So here's a better solution.

Let's say you want to test under Pythons version 2.5, 2.6 and 2.7, and you already have an interpreter for each version installed. For example, on my current development machine, python 2.7 is at $HOME/opt/python2.7/bin/python2.7, python 2.6 is at /usr/bin/python2.6, and python 2.5 is at /usr/bin/python2.5.

What you can do is create a subdirectory for each version in some convenient location. I name them like "env_pythonX.Y" and just place them under $HOME/bin (where X and Y are the major and minor version numbers). Then, inside each of these directories, make a symbolic link named "python" to the appropriate version.

Again using my dev machine as an example, $HOME/bin/env_python2.7/python is a symbolic link to $HOME/opt/python2.7/bin/python2.7, $HOME/bin/env_python2.6/python is a symbolic link to /usr/bin/python2.6, and $HOME/bin/env_python2.5/python is a symbolic link to /usr/bin/python2.5.

Now you're prepared. To run the script program.py from the Unix command line, using (say) python version 2.6, just define the PATH so that $HOME/bin/env_python2.6/python is first. At the prompt, you can type something like:

PATH=$HOME/bin/env_python2.6:$PATH ./program.py

Here, I've used a syntax provided by most modern shells for per-command variable assigments. Prepending a command with FOO=BAR means "Run the command in an environment where the variable FOO has the value BAR". This conveniently lets us set the search path to produce our python, for just that one invokation.

This is easy to repeat with the others. To manually run the script with each Python version in sequence, use this series of commands:

PATH=$HOME/bin/env_python2.7:$PATH ./program.py
PATH=$HOME/bin/env_python2.6:$PATH ./program.py
PATH=$HOME/bin/env_python2.5:$PATH ./program.py

Of course, this is all highly automatable in your test scripts.

By the way, if the symbolic link is wrong, a different python version may be silently used. You can quickly verify with python -V:

PATH=$HOME/bin/env_python2.6:$PATH python -V

This should emit something like Python 2.6.5.

You're all set. Now get to testing!

2. Using the Python Interpreter — Python 3.8.5 documentation, If you have the py.exe launcher installed, you can use the py command. When a script file is used, it is sometimes useful to be able to run the script and enter When commands are read from a tty, the interpreter is said to be in interactive mode. The interpreter prints a welcome message stating its version number and a� Let’s take the case of our local Python file again. If you were to save it to a local .py file named python_script.py. There are many ways to do that: Create a Python script from command line and save it; Create a Python script using a text editor or IDE and save it; Saving a Python script from a code editor is pretty easy.


3. Using Python on Windows — Python 3.8.5 documentation, It can be used to build Python packages or run scripts, but is not updateable and has Changed in version 3.6: Support for long paths was enabled in Python. Step 2: Invoke the Python interpreter to run the script. Python’s interpreter can be invoked simply by writing keyword “python” on command prompt. In order to execute a script we need to accompany the keyword “python” with the name of the script enclosed inside a pair of double quotes. Like this: D:\Python Tutorials>python "tut1.py"


Installing Python Modules (Legacy version) — Python 2.7.18 , You don't need to run Python or a setup script, you don't need to compile Running setup.py install builds and installs all modules in one run. Changed in version 2.4: The --home option used to be supported only on Unix. How to Run Python Scripts Interactively. It is also possible to run Python scripts and modules from an interactive session. This option offers you a variety of possibilities. Taking Advantage of import. When you import a module, what really happens is that you load its contents for later access and use.


What's New In Python 3.9 — Python 3.9.0b5 documentation, More generally, try to run your tests in the Python Development Mode which helps to prepare your code to make it compatible with the next Python version. In Python 3.9 only, you can switch back to the LL(1) parser using a command line � Function in the 'trace' module in Python library generates trace of program execution, and annotated statement coverage. It also has functions to list functions called during run by generating caller relationships. Following two Python scripts are used as an example to demonstrate features of trace module.