I just installed Python 2.7, but IDLE is currently broken on OS X 10.6.4. Is there anyway I can revert to the earlier, Apple installed, version? A simple PATH adjustment, perhaps?
Right now $PATH looks like this for me:
/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin:
/usr/bin/python is where Apple puts (the symlink to) the system version of Python -- so, just remove that first item from the PATH, and you should be fine.
The default version is in /usr/bin, so just do a
export PATH=/usr/bin:$PATH
(Adjust the command according to your choice of shell)
It is simply a matter of setting the path. Look in /Library/Frameworks/Python.framework/Versions/ for the different versions
I have the following aliases in my .profile
alias python25="export PATH=/usr/bin:${PATH}"
alias python26="export PATH=/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
alias pythonepd="export PATH=/Library/Frameworks/Python.framework/Versions/6.2/bin:${PATH}"
Switching between versions is then just a matter of a simple command.
If you want to continue to use Python 2.7, just replace it using the other, 32-bit only (10.3 and above) OS X installer available at the python.org download link. IDLE for 2.7 is only broken when using the 10.5 and above 64-bit installer; see Issue 9227.
If you really do want to remove Python 2.7 as your default Python, you'll need to undo the PATH change that the Python Installer makes by default to various shell login scripts, ~/.bash_profile or ~/.profile. It leaves the original files as ~/.bash_profile.pysave and ~/.profile.pysave. So you can compare them and just move the original back. For example, if your login shell is bash:
$ diff .bash_profile{,.pysave} # does it look ok?
$ mv .bash_profile.pysave .bash_profile
Related
I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.
When I run python in cmd, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.
import sys
print (sys.version)
What's going on here?
This is if you have both the versions installed.
Go to This PC โ Right-click โ Click on Properties โ Advanced System Settings.
You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.
You will see a top half for the user variables and the bottom half for System variables.
Check the System Variables and double-click on the Path (to edit the Path).
Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.
Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.
The Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.
Newer versions of Python update the launcher. The latest version has a py -0 option to list the installed Pythons and indicate the current default.
Here's how to check if the launcher is registered correctly from the console:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*
Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.
For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.
py.exe can also be run with switches to force a Python version:
py -3 script.py # select latest Python 3.X version to be used.
py -3.6 script.py # select version 3.6 specifically.
py -3.9-32 script.py # select version 3.9 32-bit specifically.
py -0 # list installed Python versions (latest PyLauncher).
Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.
Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'
py -2
If you need to switch from python 2.x to python 3.x you will have to use '-3' switch
py -3
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.
If you know about Environment variables and the system variable called path, consider that any version of any binary which comes sooner, will be used as default.
Look at the image below, I have 3 different python versions but python 3.8 will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)
If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).
As a user
Always type py instead of python when running a script from the command line.
Setup your "Open with..." explorer default program association with C:\Windows\py.exe
Set the command line file extension association to use the Python Launcher for Windows (this will make typing py optional). In an Admin cmd terminal, run:
ftype Python.File="C:\Windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.11). You can see what version of python is your default by typing py. You can also set PY_PYTHON3 or PY_PYTHON2 to specify default python 3 and python 2 versions (if you have multiple).
If you need to run a specific version of python, you can use py -M.m (where M is the major version and m is the minor version). For example, py -3 will run any installed version of python 3.
List the installed versions of python with py -0.
As a script writer
Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:
#!/usr/bin/env python3
Note: (see this question) If python3 does not work for you, ensure that you've installed python from the Windows Store (e.g. via winget install --id 9NRWMJP3717K, as the winget package Python.Python.3.11 does not appear to include a python3.exe).
You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).
See also
PEP 397 -- Python launcher for Windows
PEP 486 -- Make the Python Launcher aware of virtual environments
Python Launcher for Windows - User Guide
See here for original post
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c
Thus, on my system I made a py.ini file under c:\windows\ where py.exe exists, with the following contents:
[defaults]
python=3
Now when you Double-click on a .py file, it will be run by the new default version. Now I'm only using the Shebang #! python2 on my old scripts.
Edit registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
Set default program to open .py files to python.exe
This work for me.
If you want to use the python 3.6 you must move the python3.6 on the top of the list.
The same applies to the python2.7
If you want to have the 2.7 as default then make sure you move the python2.7 on the very top on the list.
step 1
step 2
step 3
then close any cmd command prompt and opened again, it should work as expected.
python --version
>>> Python 3.6
This worked for me:
Go to
Control Panel\System and Security\System
select
Advanced system settings from the left panel
from Advanced tab click on Environment Variables
In the System variables section search for (create if doesn't exist)
PYTHONPATH
and set
C:\Python27\;C:\Python27\Scripts;
or your desired version
You need to restart CMD.
In case it still doesn't work you might want to leave in the PATH variable only your desired version.
With Python version 2.7, 3.7, 3.9 and 3.11 installed on my Windows 11 OS, the above solutions didn't works for me.
The command py --help give some hints to set python version, example:
usage:
[...]
If an exact version is not given, using the latest
version can be overridden by any of the following, (in priority
order):
๐ From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini
The easiest way I found to set a specific default Python version is to create/edit a py.ini file under %LOCALAPPDATA%\py.ini.
Content of py.ini ๐
[defaults]
python=3.7
Console output with Administrator and User right ๐ก:
C:\Users\bob>py --version
Python 3.7.9
If you are on Windows, use the ASSOC command to change the default python version for python programs.
assoc .py=<Python 3.1 directory>
Now that Python 3.3 is released it is easiest to use the py.exe utility described here:
http://www.python.org/dev/peps/pep-0397/
It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.
The easiest way to get this utility is to install Python 3.3 or later.
Nothing above worked, this is what worked for me:
ftype Python.File=C:\Path\to\python.exe "%1" %*
This command should be run in Command prompt launched as administrator
Warning: even if the path in this command is set to python35, if you have python36 installed it's going to set the default to python36. To prevent this, you can temporarily change the folder name from Python36 to xxPython36, run the command and then remove the change to the Python 36 folder.
Edit: This is what I ended up doing: I use Python Launcher.
https://stackoverflow.com/a/68139696/3154274
Check which one the system is currently using:
python --version
Add the main folder location (e.g. C/ProgramFiles) and Scripts location (C/ProgramFiles/Scripts) to Environment Variables of the system. Add both 3.x version and 2.x version
Path location is ranked inside environment variable. If you want to use Python 2.x simply put path of python 2.x first, if you want for Python 3.x simply put 3.x first
This uses python 2
Since my problem was slightly different and none of the above worked for me, I'll add what worked for me. I had installed the new python launcher for python 3.10 today, installed the version through it, but the command window did not recognise the version. Instead, it listed older python3 versions I had on my computer.
Finally, in the windows programs list, I saw that I had two versions of the python launcher. I uninstalled the old one, and now python 3.10 shows up correctly when running py -0 and is the chosen version when running py.
Apologies if this is a noob answer, I am new to all this.
I had same problem and solve it by executing the installation file again. when you do that python automatically knows you have installed it before so it recommends you 3 options! select modify and select all packages you want to modify then in the next page you can check if new version of python is added to your environment variables or not. check it and then execute modification. I did and it solved.
Use SET command in Windows CMD to temporarily set the default python for the current session.
SET PATH=C:\Program Files\Python 3.5
Try modifying the path in the windows registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).
Caveat: Don't break the registry :)
Recently, I installed miniconda, and because of which, my default python directory seems to have changed to,
/home/user/miniconda3/bin/python
Also, the default version seems to have changed to python 3.7 instead of python 2.7
And I cannot seem to install various libraries like tensorflow.
How do I get my python back to normal?
The miniconda installation has properly added itself to your PATH. You can check whether /home/user/miniconda3/bin is listed when you run
echo $PATH
in a terminal. If so, it most likely added some lines to the bottom of your ~/.bashrc file. Simply remove these lines yourself, and your PATH should be back to normal (save the file and open up a new terminal), meaning that typing e.g. python will give you the system Python.
I just upgrade python3 to 3.6 (using homebrew) and now VS Code doesn't seem to be aware of it. When I try to select my Python interpreter I see 2.7.9 and 2.7.10 (in /usr/bin and /usr/local/bin respectively) but I do not see 3.6.3 (/usr/local/bin/python3).
It's certainly in my path, and I'm aware that I can update settings.json manually, but I use both Python 2 and 3 for various projects and making them available via the interpreter switcher would be incredibly useful. Any ideas why Code isn't autodetecting python3? Or is there a way for me to force-add it to the list?
Figured it out. Something I clearly installed had modified by .bash_profile to include the following:
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
I commented that out, restarted Code, and now I can see all my Python interpreters listed (including 3.6.3).
In VS Code, you're able to easily set the interpreter you'd like to use for Python. Follow this official guide.
From the site:
To select a specific interpreter, select the Python: Select Interpreter command from the Command Palette (โงโP).
I'm looking for a way to cleanly uninstall all versions of python on OS X 10.10 except the default version that followed with the Mac. How do I proceed?
Currently I've some weird behaviour. When typing which -a python I get the following output:
/Users/harisfawad/anaconda/bin/python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/local/bin/python
/usr/bin/python
Why doesn't the default version of python in /System/Library/.../ show up here? I can locate that version in Finder, so I know that it isn't deleted.
Also, echo $PYTHONPATH returns an empty line.
Can I just go ahead and delete all these folders and expect the default version to run correctly?
I've read a similar inquiry here http://bugs.python.org/issue7107 but I'm not sure if this is still applicable to python versions 2.7/3.4.
This is also a similar question How to uninstall Python 2.7 on a Mac OS X 10.6.4? But again, I don't want to be careless and do the uninstalling before making sure that the system provided version works.
UPDATE: When running brew doctor I get this Warning: "config" scripts exist outside your system or Homebrew directories. And it goes on listing the "config" files in /.../anaconda/bin and in /Library/.../bin.
UPDATE2: I've successfully gone back to the default version of python that was included in OS X. All the above versions of python where moved to trash, except /usr/bin/python. Thanks, #rhashimoto and #PadraicCunningham!
The file /usr/bin/python (and /usr/bin/pythonw, which is a hard link to the same file) is actually a launcher program that invokes the default version of Python from /System/Library/Frameworks/Python.framework/Versions. You can select the version (2.6 and 2.7 in Yosemite) the launcher invokes using either the defaults command or the VERSIONER_PYTHON_VERSION environment variable.
If you want to run a specific version manually, you can invoke /usr/bin/python2.6 or /usr/bin/python2.7, which are symbolic links into /System/Library/Frameworks/Python.framework/Versions.
You should be fine removing all other Python implementations you list from your path, including /Library/Frameworks/Python.framework/Versions/2.7/bin/python (not sure how you got that one). It would still be wise to move them somewhere (e.g. your trash folder) to test your change before deleting them permanently.
I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.
When I run python in cmd, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.
import sys
print (sys.version)
What's going on here?
This is if you have both the versions installed.
Go to This PC โ Right-click โ Click on Properties โ Advanced System Settings.
You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.
You will see a top half for the user variables and the bottom half for System variables.
Check the System Variables and double-click on the Path (to edit the Path).
Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.
Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.
The Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.
Newer versions of Python update the launcher. The latest version has a py -0 option to list the installed Pythons and indicate the current default.
Here's how to check if the launcher is registered correctly from the console:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*
Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.
For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.
py.exe can also be run with switches to force a Python version:
py -3 script.py # select latest Python 3.X version to be used.
py -3.6 script.py # select version 3.6 specifically.
py -3.9-32 script.py # select version 3.9 32-bit specifically.
py -0 # list installed Python versions (latest PyLauncher).
Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.
Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'
py -2
If you need to switch from python 2.x to python 3.x you will have to use '-3' switch
py -3
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.
If you know about Environment variables and the system variable called path, consider that any version of any binary which comes sooner, will be used as default.
Look at the image below, I have 3 different python versions but python 3.8 will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)
If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).
As a user
Always type py instead of python when running a script from the command line.
Setup your "Open with..." explorer default program association with C:\Windows\py.exe
Set the command line file extension association to use the Python Launcher for Windows (this will make typing py optional). In an Admin cmd terminal, run:
ftype Python.File="C:\Windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.11). You can see what version of python is your default by typing py. You can also set PY_PYTHON3 or PY_PYTHON2 to specify default python 3 and python 2 versions (if you have multiple).
If you need to run a specific version of python, you can use py -M.m (where M is the major version and m is the minor version). For example, py -3 will run any installed version of python 3.
List the installed versions of python with py -0.
As a script writer
Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:
#!/usr/bin/env python3
Note: (see this question) If python3 does not work for you, ensure that you've installed python from the Windows Store (e.g. via winget install --id 9NRWMJP3717K, as the winget package Python.Python.3.11 does not appear to include a python3.exe).
You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).
See also
PEP 397 -- Python launcher for Windows
PEP 486 -- Make the Python Launcher aware of virtual environments
Python Launcher for Windows - User Guide
See here for original post
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c
Thus, on my system I made a py.ini file under c:\windows\ where py.exe exists, with the following contents:
[defaults]
python=3
Now when you Double-click on a .py file, it will be run by the new default version. Now I'm only using the Shebang #! python2 on my old scripts.
Edit registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
Set default program to open .py files to python.exe
This work for me.
If you want to use the python 3.6 you must move the python3.6 on the top of the list.
The same applies to the python2.7
If you want to have the 2.7 as default then make sure you move the python2.7 on the very top on the list.
step 1
step 2
step 3
then close any cmd command prompt and opened again, it should work as expected.
python --version
>>> Python 3.6
This worked for me:
Go to
Control Panel\System and Security\System
select
Advanced system settings from the left panel
from Advanced tab click on Environment Variables
In the System variables section search for (create if doesn't exist)
PYTHONPATH
and set
C:\Python27\;C:\Python27\Scripts;
or your desired version
You need to restart CMD.
In case it still doesn't work you might want to leave in the PATH variable only your desired version.
With Python version 2.7, 3.7, 3.9 and 3.11 installed on my Windows 11 OS, the above solutions didn't works for me.
The command py --help give some hints to set python version, example:
usage:
[...]
If an exact version is not given, using the latest
version can be overridden by any of the following, (in priority
order):
๐ From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini
The easiest way I found to set a specific default Python version is to create/edit a py.ini file under %LOCALAPPDATA%\py.ini.
Content of py.ini ๐
[defaults]
python=3.7
Console output with Administrator and User right ๐ก:
C:\Users\bob>py --version
Python 3.7.9
If you are on Windows, use the ASSOC command to change the default python version for python programs.
assoc .py=<Python 3.1 directory>
Now that Python 3.3 is released it is easiest to use the py.exe utility described here:
http://www.python.org/dev/peps/pep-0397/
It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.
The easiest way to get this utility is to install Python 3.3 or later.
Nothing above worked, this is what worked for me:
ftype Python.File=C:\Path\to\python.exe "%1" %*
This command should be run in Command prompt launched as administrator
Warning: even if the path in this command is set to python35, if you have python36 installed it's going to set the default to python36. To prevent this, you can temporarily change the folder name from Python36 to xxPython36, run the command and then remove the change to the Python 36 folder.
Edit: This is what I ended up doing: I use Python Launcher.
https://stackoverflow.com/a/68139696/3154274
Check which one the system is currently using:
python --version
Add the main folder location (e.g. C/ProgramFiles) and Scripts location (C/ProgramFiles/Scripts) to Environment Variables of the system. Add both 3.x version and 2.x version
Path location is ranked inside environment variable. If you want to use Python 2.x simply put path of python 2.x first, if you want for Python 3.x simply put 3.x first
This uses python 2
Since my problem was slightly different and none of the above worked for me, I'll add what worked for me. I had installed the new python launcher for python 3.10 today, installed the version through it, but the command window did not recognise the version. Instead, it listed older python3 versions I had on my computer.
Finally, in the windows programs list, I saw that I had two versions of the python launcher. I uninstalled the old one, and now python 3.10 shows up correctly when running py -0 and is the chosen version when running py.
Apologies if this is a noob answer, I am new to all this.
I had same problem and solve it by executing the installation file again. when you do that python automatically knows you have installed it before so it recommends you 3 options! select modify and select all packages you want to modify then in the next page you can check if new version of python is added to your environment variables or not. check it and then execute modification. I did and it solved.
Use SET command in Windows CMD to temporarily set the default python for the current session.
SET PATH=C:\Program Files\Python 3.5
Try modifying the path in the windows registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).
Caveat: Don't break the registry :)