Generally i was using the latest python version 3.12. in my terminalk
So when i created a virtual environment with
python -m venv xyz
the created virtual environment was under 3.12
For testing purposes it was now necessary to also install python version 3.9.
But now when i am using venv to create a new virtual environment it is the version 3.9.
How can i change the "standard" python-version bakc to 3.12. when i am creating a virtual environment using venv?
This is most likely due to the path variable. You can see this in a command window by typing
Path
at your prompt.
To update the settings,
- Open the 'System' properties.

- Open Environment Variables

- Highlight the 'Path' Variable and click edit.

- Edit the values for the Python entries, to point to the desired python version.

- OK on all boxes, close any CMD windows open, and open new one. Python command should now reference the correct location.
Solution refers to Windows 10, but is essentially the same on Windows 7 +
I was having the same issue as the older python executable was in my system space (which I have no access to) and the newer version is in the user space.
The work around I thought of was to create a .bat file which will open a CMD window in the user space python version.
new_python.bat:
@set "PATH=C:\Users\USER\AppData\Local\Programs\Python\Python39\;C:\Users\USER\AppData\Local\Programs\Python\Python39\Scripts\;%PATH%"
@cmd /k python --version
Does a decent job for me. Just change the first two lines in the PATH variable to your Python directory and the Python Scripts directory. The CMD which will open using this bat will be ready to run the newer version of python executable.
For running Python 3 on your computer from the Command Prompt: enter py -3.6 myfile.py
For running Python 2.7: py -2.7 myfile.py
This also works if you have Python 3.4 and Python 3.6 installed, just change the argument.
Easy solution. My last installation was python 3.6 (folder name Python36-32). I just changed folder name to something like Python36-32- and now python 2.7 running.
I have both 3.10 and 3.11b3 installed on my windows 10 machine. I'd like py.exe to launch 3.10.
I had read that I should create py.ini and pyw.ini in both \windows and C:\Users<user>\AppData\Local\Programs\Python\Launcher\ and the files should contain:
[defaults] python=3.10
I've done this, but py.exe launches 3.11b3. I don't have any other py.ini files. How do I fix this so \windows\py.exe launches my preferred default version?
EDIT: see https://stackoverflow.com/questions/68121982/multiple-python-versions-installed-how-to-set-the-default-version-for-py-exe
I could use py -3.10 to launch, but from the link there should be a way to just use py
I'm on windows, and I primarily use python 3, but for a couple modules, I still use python 2.7.x. When I'm in command prompt and type "python", it defaults to python 2. How do I change this do default to python 3 and how do I create an alias for another work i create to run python 2?
You can use explicit paths:
c:\python26\python setup.py install
c:\python25\python setup.py install
Recent versions of Python install PyLauncher. It is installed in the path so no need to add an explicit Python to the path, and it allows easy switching between multiple Python versions.
Examples:
py -3 setup.py # run latest Python 3
py -2 setup.py # run latest Python 2
py -3.3
py -2.7-32 # use 32-bit version
py # run default version
The default version can be specified in the environment variable PY_PYTHON, e.g. PY_PYTHON=3 (latest Python 3).
It depends on your operating system. If you have python 2.6 installed, you need to change your environment path to point to the 2.6 executable rather than the 2.5 executable. Do a Google search for changing your PATH variable on your operating system.
You can run multiple python versions on your machine.
To make Python 3.6 default, just put path of Python 3.6 before path of Python 3.4 in system variables.
OR
In python34 directory on C drive rename python.exe to python3.4.exe. After doing this if you run command python in command prompt it will open python 3.6. And to run python 3.4 you need to run command python3.4 as we have renamed it to python3.4.exe in python34 directory.
After making changes in system variables in environment variables you just need to restart your command prompt. No need to restart machine.
After doing a restart of my machine after deleting python34 from my system path variables as @Talha Junaid mentioned in the comments, my python now defaults to python3.6 when accessed through command prompt, windows must load all the path variables on startup and hold them in memory until a restart occurs.
Steps for anyone else starting the process.
Windows 10
Type "System" into the search bar to go to system settings in the control panel (Control Panel\All Control Panel Items\System)
Click "Advanced System Settings" -> "Environment Variables"
For User Variables:
- Highlight the row for "Path" by clicking on it and then click the "Edit" button
- Highlight by clicking the version of python you want to remove from your environment variables and then click the "Delete" button.
Repeat steps 3 and 4 for System Environment Variables.
Close all the windows by clicking the "OK" buttons
Restart your machine
If anyone is looking for this in 2023:
Goto your Environment Variables....
Under System variables, look for the variable PY_PYTHON. If it exists, you can change its value to the version of Python you want to use by default (for example, 3.10 or 3.11). If it doesn't exist, you can create it by clicking on New..., entering PY_PYTHON as the variable name, and the desired Python version as the variable value.
Click OK to apply the changes.
To verify your changes, open a new command prompt and type py. It should start the Python interpreter in the version you specified.
To set the default version of Python, you need to have installed Python Launcher (cf below for some info). If you didn't, you can probably install it by using the installation .exe of one of your Python installations, and selecting modifying/repair.
You also need to know where your Python Launcher is:
- if you installed it for all users, it is in
C:\WINDOWS\, - if not, it is in
%USERPROFILE%\AppData\Local\Programs\Python\Launcher\.
In this example, the latest installed version of Python is Python 3.11 but the version that should be the default one is Python 3.6.
To set a default Python version:
go to your Python Launcher Folder and create 2 files:
py.iniandpyw.inimake sure that both of these files are UFT-8 (they shouldn't be UFT-8 with BOM)
add to each of these files:
[defaults] python=3.6now, right click on any
.py, selectopen with, selectcheck another app, checkAlways use this app..., scroll all the way down and click onmore app...and navigate to your Python Launcher folder and select thepy.exe.do the previous step again but this time, right-click on a
.pywfile and select thepyw.exe.
How to use it
● in a CMD:
Do not use anymore
pythoninCMD, always usepy. If you type justpy, then it will open a Python 3.6 console. If you want a Python 3.11 console, typepy -3.11.As indicated in the comment by @karl-knechtel: If you have an active
venv, using python instead ofpy` will prioritize the venv's Python, which is generally what you want.To use pip to install :
py -3.11 -m pip install # will install package for 3.11 py -m pip install # will install package for 3.6
● For python files:
- If the file doesn't contain a shebang, then it will be started with the default Python version which is
3.6. - To use
Python 3.11for this file, add the shebang#!python3.11at the very top of your script
To verify that everything is working:
open a
CMD, writepy -0pand look for the*. It should be next to the default version you have set in the.inifiles.Installed Pythons found by py Launcher for Windows
-3.11-64 C:\Users<user>\AppData\Local\Programs\Python\Python39\python.exe
-3.6-64 C:\Users<user>\AppData\Local\Programs\Python\Python36\python.exe *With this setup, you can use the specific version of Python that you need in your scripts:
3.6 (as it will call the default version)
import sys print(sys.version_info) input("close")3.6 (as it will call the default)
#!python import sys print(sys.version_info) input("close")3.11
#!python3.11 import sys print(sys.version_info) input("close")3.11 (as it is the latest python 3 installed)
#!python3 import sys print(sys.version_info) input("close")
Some info about the Python launcher from docs.python.org
The Python launcher for Windows is a utility which aids in locating and executing of different Python versions. It allows scripts (or the command-line) to indicate a preference for a specific Python version, and will locate and execute that version. Unlike the PATH variable, the launcher will correctly select the most appropriate version of Python. It will prefer per-user installations over system-wide ones, and orders by language version rather than using the most recently installed version.
Two .ini files will be searched by the launcher - py.ini in the current user’s “application data” directory [...] and py.ini in the same directory as the launcher. The same .ini files are used for both the ‘console’ version of the launcher (i.e. py.exe) and for the ‘windows’ version (i.e. pyw.exe).
Customization specified in the “application directory” will have precedence over the one next to the executable, so a user, who may not have write access to the .ini file next to the launcher, can override commands in that global .ini file.
⚠ Your python version can be installed either for all user (python.exe will be in program files) or for the profile (python.exe will be in appdata). If it doesn't work, try uninstalling all python version, and the launcher and reinstall everything as current user to start clean. To do that, download the .exe of your current Python installation, run it and click on uninstall (dont delete the remaining folder in app data if you want to keep your python packages), then reinstall it. If you install the launcher for all users but python for current user, your python.exe will be in appdata (a folder for each version), and your launcher will be C:\Windows\py.exe
If you want to install it for all "all users" and the check box is grayed out: you need first to uninstall the python launcher. If it's still grayed uninstall the python version(s) that remains (everything wil be in program files).

As you can see, I have both Python2 and Python3 installed.

I hope you know that the path of the python executable has to be added to the PATH environment variable in Windows. As you can see, the path of Python2 is placed above the path of Python3 in my system.
How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM.
Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.
Both Python2 and Python3 executables have the same name in Windows so it never runs python3.
What seems like an obvious solution?
You might think, changing the name of python.exe to python3.exe for the Python3 executable will solve your problem. You are partially right, it will work. But you have to use python3 file.py or python3 --version, which I think, is understandable. But, pip will no longer work if you change the name of the original python executable. You will no longer be able to install external packages and modules.
How can this problem be solved?
You can create an alias for the Python3 executable called python3.bat.
.exe and .bat files can be called from the cmd directly without using their extension. We always write python filename.py instead of python.exe filename.py although both are correct. The same can be done with .bat files.
Go back to the first image and notice the python3.bat file below python.exe. That is my way of calling python3 without renaming my original python executable.
python3.bat
Create a new file using notepad or something and paste this %~dp0python %*
I don't fully understand how this works except that dp0 basically runs python from inside the same directory and %* passes all the arguments to the python executable. Place this file inside your Python3 installation directory and your problem will hopefully be solved.

python3 basically runs your python3.bat file, which in turn runs the python.exe from its folder and passes the arguments to it.
I hope this solves your problem.
You should have python3 also in your path. You could use python3 to run your script:
python3 <your_script>
But to answer your question, you could use alias and update it as python.
$ python --version
Python 2.7.6
$ alias python=python3
$ python --version
Python 3.6.4