Python update - how to update `py` to point to new version? - python

I started learning Python on v3.7, and now v3.8 is out.
I've downloaded 3.8.0 and installed it to a new python38 directory alongside my python37 folder. The path was updated and now includes both python v3.7 and v3.8.
How to update py?
If I open up a new Command Window or Git Bash window, python --version is updated, but not py --version, how do I update py to point to the latest version?
Are there any other considerations with respect to virtual environments?
They should remain pointing to the version of python they were built with - is that true of both python and py when a venv is active?
Note: py is a command-line python launcher tool included with Python for Windows as of v3.3

After searching for a while, I stumbled into the answer for the py update part, at least.
The py command is an executable included with Windows Python distributions starting with Python v3.3. It is called the "Python Launcher for Windows" in the official Python documentation.
The documentation on the Python Launcher is pretty complete, but to answer my original question, I had set the environment variable PY_PYTHON to 3.7 to declare the default Python version for py to use.
I was able to fix the issue by updating this to 3.8 and restarting any terminal and program that uses a terminal to access py/python. There are other Python environment variables so I updated them all:
UPDATE: I was able to completely remove the PYTHONHOME and PYTHONPATH variables and just use PY_PYTHON to control the default version. You can read more about these variables in the Environment Variables section of the Python Documentation
set PY_PYTHON to 3.8
set PYTHONHOME to my new Python38 directory
for PYTHONPATH added the new python38 and python38/scripts directories.
Note: After updating environment variables, you will need to restart some programs, and others might require either a logout or full reboot (was true of VS Code on Windows).
More info on py command and versions
You can get a list of basic py commands using py --help:
$ py --help
Python Launcher for Windows Version 3.8.150.1013
usage:
C:\WINDOWS\py.exe [launcher-args] [python-args] script [script-args]
Launcher arguments:
-2 : Launch the latest Python 2.x version
-3 : Launch the latest Python 3.x version
-X.Y : Launch the specified Python version
The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32 : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64 : Launch the latest 64bit Python X version
-0 --list : List the available pythons
-0p --list-paths : List with paths
The version strings above used to launch a particular version of python are what can be put into the PY_PYTHON environment variable. For example, if you just wanted the latest Python3, then use 3, for a particular Python3, use 3.8, or for the 32-bit version, use 3.8-32, etc.

Related

Windows - Use default python rather than Anaconda installation

my problem is before I installed Anaconda, my python with python and py commands shows the same versions. After I installed Anaconda, my python version is using Anaconda installation. How to prevent this, because I don't want to use Anaconda python version on my Windows.
I already put my python PATH on the top
When I check the PATH, there's no anaconda PATH anywhere when I done my installation
My python version will use the default python version, not the Anaconda's version
You need to change the default opener for the .py files on your computer.
try to right-click on the .py file, select "open with" and look for python.
You can always use cmd and run the following:
python <path_to_file>
or
python3 <path_to_file>
It depended on your python version
(as long as you set your python path in windows Path)

Windows 10: Make Py Launcher choose official installation instead of Anaconda (same Python version)

I installed official Python then Anaconda3 afterwards (both 3.9)
I suspect it is because I ticked 'Register Anaconda3 as my default Python 3.9' when I was installing Anaconda, that now when I type py -0p in cmd, the path goes to Anaconda (I do not have Anaconda in PATH). I would like Python 3.9 to use the official installation.
Installed Pythons found by py Launcher for Windows *
-3.9-64 C:\Users\xxx\anaconda3\python.exe
What exactly did ticking 'Register Anaconda3 as my default Python 3.9' change in my system? I would like to revert it. I already looked for py.ini and it does not exist beside py.exe and %LOCALAPPDATA%, so it is not that.
With reference to this comment: py launcher does not find my Python 2.7, I deleted \HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.9 in regedit. py -0p gave this output after a reboot, which is what I wanted:
Installed Pythons found by py Launcher for Windows *
-3.9-64 C:\Program Files\Python39\python.exe
My Python installation is system-wide, so the keys for the offical installation exist at \HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.9.
For those who installed Python for their user only, you may want to modify the keys in \HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.9 instead of deleting them.

Pycharm Project default interpreter does not match my Python system default installation?

I am a little confused about Python installations, configurations and interpreter locations.
I am using Ubuntu 20.04 and I have been using Pycharm for some time, installed both Python 2.7 and Python 3.8 (even though I am not sure where I installed both...).
Now I started a project with Pycharm that uses some specific packages that I included in the requirements.txt file. Every time I open Pycharm it asks me to install those packages (Package requirements 'numpy~=1.16.6', 'scipy~=1.5.1' are not satisfied. Install requirements/Ignore requirements), I do it and the next time I get the same request.
If I try to set up a Python interpreter through the GUI of Pycharm I get the following options:
Project Default (Python 2.7) /usr/bin/python
Python 2.7 /usr/bin/python
Python 3.8 (OldProject) ~/Workspace/OldProject/venv/bin/python
Python 3.8 (OldProject2) ~/Workspace/OldProject2/venv/bin/python
How can I change the system default Python to version 3.8?
If I run on the terminal:
python --version
I get
Python 3.8.2
So why is Pycharm displaying Python 2.7 as Project Default? And why there's no Python 3.8 system installation among the options?
You can change your Pycharm python interpreter by going in to File -> Settings and than search python interpreter at the search input.
The command line python version is the one that the "Path" envirment variable refer too (the file python.exe in the relevent python folder)

Windows: How to configure multiple versions of Python to work together?

I installed Python 2.7 a long time ago on my PC (I am running Windows 10). Today I decided to install Python 3.7, but after typing 'python' into the command prompt the console printed 'python 2.7...'
I have no idea as to what I should do. Would this be a problem with my path configuration? I considered uninstalling python 2.7 but I still want it installed on my computer.
Configure multiple Python versions on Windows
Python 3.3 introduced the Python Launcher for Windows. Rather than using python.exe, call py and select the version with flags:
py -2.7
py -3
System-wide installations of Python 3.3 and later will put the launcher on your PATH.
Note on Virtual Environments (new in Python 3.5)
If the launcher is run with no explicit Python version specification, and a virtual environment (created with the standard library venv module or the external virtualenv tool) active, the launcher will run the virtual environment’s interpreter rather than the global one. To run the global interpreter, either deactivate the virtual environment, or explicitly specify the global Python version.
Outdated Method (not recommended)
Rename executables - After installing multiple versions, change the names of the executables. For example, for Python 2.7, under the installation folder, rename python.exe to python27.exe and rename Python 3.7 from python.exe to python37.exe. Then on the command line, select the version by entering python27 or python37. Whichever version is preferred, could be left as just python.
Add Path Environment Variables - For example, on Windows 10, go to the Windows menu and search for "environment variables" and click edit the system environment variables. In the System Properties dialog, click Environment Variables.... Under "System variables", select "Path". Click Edit.... Click New and add the first entry below. Click New again for each Path variable entry.
C:\Python27
C:\Python27\Scripts
C:\Python37
C:\Python37\Scripts
This will enable Python and pip. Be sure that paths match your actual installation directories.
I would suggest using pyenv
I have been using it and is working well for me. Some of the handy features of pyenv are
It allows installing multiple version on python easily
It allows switching python version with one command in global, shell or folder level
It also allows creating virtual env using virtualevn extension
I would suggest to use virtual environment. Soon or later you would you might get dependency problems.
You Can Configure The Python2 & python3 In Windows PC Like This:
First Of All Install Python 2 & Python 3 in windows PC in C directory like this:
C:\Python27 --> For Python2
C:\Python39 --> For Python3
After Installing Both Packages Go To Their Respective Folders And Copy python.exe & Paste In The Same Directory and rename the python - Copy with python2(In python27 folder) & python3(In python39 folder)
Python2:
Python3:
And Then Set Environment Vairable Like This:
All Done Now You Can Run Any Script Which May Compatible With Python2 or Python3 :)

Do I have to uninstall old python versions to update to a new one on Windows?

I have python 3.2 installed and I want to know if I have to uninstall earlier versions before installing newer ones.
Install all the Python X.Y versions you want, but include Python 3.3 or later (last is best, or reinstall it after all the others) to get the Python Launcher that #abarnert mentioned in his comments. It is also available as a standalone installer.
Earlier versions than 3.3 should not register extensions, so the launcher installed by 3.3 or later will remain the default handler for .py files.
From the command line:
py -2 # launch latest 2.x version installed.
py -3 # latest 3.x
py -3.2 # run exact version
py -3.2-32 # run 32-bit version on 64-bit system.
So you can even have mixed 32-bit and 64-bit installations.
The environment variable PY_PYTHON can be set to specify the default Python to run.
In scripts, add a comment of the following forms below to use that version of Python when the script is double-clicked or run from command line via py script.py:
#!python2
#!python3
#!python3.2
#!python3.2-32
See PEP 397 for further details.
You can install multiple versions of Python on Windows, but only the last version you installed will be used by default: when double-clicking a .py file in Windows Explorer, when typing just python at the command line, etc. "Edit in IDLE" on the context menu also uses the last version you installed. To use other versions you'll need to specify the full path of the version you want. Also, if you use the PYTHONPATH environment variable, there's only one of those, and the scripts in the directories specified in PYTHONPATH may or may not work with whatever version of Python you happen to be running. This can be worked around by writing a batch file that sets PYTHONPATH before launching Python.
It generally depends on what OS you are running and how you installed your python. Under linux or Mac OSX, you don't need to unistall the previous version. I am not sure how things are handled for Windows.

Categories

Resources