Sorry if the question is simple or I'm missing something obvious but I'm fairly new with console commands and Python.
As the title suggests, I installed Homebrew and subsequently the latest version of Python. I want to make this the default when running python --version in bash.
I tried changing the path in my bash_profile with the following line at the bottom:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
It seems that when I install new modules however, that they are for 2.7 and not 3. Checking pip --version going straight to the default:
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
I also tried running the following commands:
echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
this does edit my bash_profile again, but the line I wanted (and added manually) isn't the one that appears; instead the following is inserted (I have no idea why Applications/VMware is inserted at the end, it has nothing to do with Python or what I'm trying to do):
export PATH=/usr/local/bin:/usr/local/sbin:$PATHexport Path=/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware
Can anyone explain to me how I can make python3 the default. I don't want to get rid of 2.7 obviously because a lot of scripts for mac still depend on it. Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
You really don't want to change this.
First:
Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
Yes! Install pipenv, and set up an environment where 3.7 is the default, but leave the system default at 2.7.
More generally:
According to PEP 394, which isn't due to be reevaluated until 2020, if you don't have a virtual environment activated:
python3 runs Python 3.x
python2 runs Python 2.x
python usually shouldn't be used—but if you do use it, it should run Python 2.x.
Usually, you'll also have, e.g., python3.7 and python2.7 to run specific 3.x and 2.x versions, although this isn't covered by any standard.
Scripts like pip, and those installed by pip, should be installed with similar suffixes—but ideally, if you have multiple versions, you shouldn't run those scripts. Most of them are designed to be run as modules, so you can run python3 -m pip to run the pip for your 3.x.
For a few things, it may be handy to set up shortcuts. (For example, instead of running python3 -m ipython from the command line, I usually use an iTerm profile that uses that instead of bash as my start command. If you don't use either IPython or iTerm, you can make a Terminal.app profile that runs python3.)
Scripts installed as part of your platform's package manager are a whole more complicated story, but macOS doesn't have an official package manager, and Homebrew generally expects you to install Python libraries with pip, not brew, so you don't have to worry about that.
Programs should have an installer—whether they install via pip or something like py2app—that creates a proper shbang line, so they run with whichever Python version they were installed with, so you don't have to worry about them. But programs that come with your OS may use something like /usr/bin/env python, and they will expect that to run the version of Python that came with your OS.
For development purposes, you usually want to use virtualenv or pipenv or conda so that python runs the Python interpreter for whichever environment is currently activated.
So, don't try to change the fact that python runs Apple's Python 2.7. That's what it's supposed to do, and if you make it do otherwise, you could break things. Use python3, python3 -m pip, etc.; use #!/usr/bin/env python3 on your scripts (or, better, use setuptools to create entry-point scripts automatically); use virtual environments; just never run python, and everything will work properly.
You just need to run command
$which python
And then goto the path rename python to python2
Then run command
$which python3
To get the path of python3
Just link with command
sudo ln -s $python3path /usr/bin
/python
You need to instead $python3path with real path
Then you can run python3 with python command
I have a specific problem with python. I have on my ubuntu two versions python3.4 and python3.6(from anaconda). I want use just anaconda pythoncurrent version
But when i run script i have some problems with another python version
another version
How i can safe delete useless python 3.4.6?
sudo apt-get remove python3.4
anaconda should have set python3 as default python3.6 version
You should probably not delete Python3 from your system, even if you have Anaconda installed, since there might be system software that:
was not tested with subsequent versions of Python;
might struggle finding Python from Anaconda.
What you should do instead is configure your IDE / environment to run Python script with Anaconda, e.g. by setting your PATH variable to point to your anaconda/bin directory or similar.
If you are using PyCharm, as it seems from the screenshot, you could set up your project to run the Anaconda Python without modifying your other command-line settings.
I bought my mac about a year ago and somehow changed my python symlink so that when I run python some_file.py, python 3.4 is used to run the file instead of python 2.7. I now need to change it back, but I can't figure out what I did to change it in the first place! When I run:
import os
os.path.realpath("/usr/local/bin/python")
in the python terminal, the output is:
'/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'
Does this not mean that my python symlink is pointing to my python 2.7 version, and not my 3.4 version? If not, how do I find out which file is run when I use the python symlink?
You probably installed that specific Python version using the official Python installer for OS X; see the Using Python on a Macintosh documentation. The installer creates the /usr/local/bin symlink for you.
If you also, at some point, had 3.4 installed then that installation is still there too. Check for a /usr/local/bin/python3 command; it'll link to the existing Python 3 binary. Use that instead to run Python 3 code.
If you do have a /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 command, you could re-create the /usr/local/bin/python symlink to point there instead, but I'd personally only use the python3 name for Python 3 scripts.
Last, you could also have used the homebrew tool to install Python; it can manage symlinks for you. However, homebrew installs Python binaries into the /usr/local/Cellar tree structure instead.
I have installed Anaconda3 just now, and I noticed that now, when I run python command from terminal, Python 3.5.1 |Anaconda 4.0.0 (64-bit)| is starting. Anaconda installer had added path to anaconda dir in $PATH and there is symlink from python to python3.5
My question is: will programs, that depends from python command and expects python2, work correctly, or I should remove symlink python from anaconda dir?
My question is: will programs, that depends from python command and
expects python2, work correctly?
Those programs should use full path of the python binary. Something like /usr/bin/python, and so $PATH is irrelevant. As long as you don't change /usr/bin/python, nothing will break.
If you remove the stuff that Anaconda has added, it's likely that Anaconda will not work properly.
That depends on your OS. Debian and Ubuntu both have ongoing projects to move the "default" version from 2 to 3 (also here). But it's not recommended to point /usr/bin/python to python3 if Python 2 is installed (see PEP 394).
If you want 'python' to be pointing to your 3.x install, you could use an alias (see here). This way you can use python in your session and at least don't change it on the whole system.
I had two versions of Python installed on my machine (versions 2.6 and 2.5). I want to run 2.6 for one project and 2.5 for another.
How can I specify which I want to use?
I am working on Windows XP SP2.
Running a different copy of Python is as easy as starting the correct executable. You mention that you've started a python instance, from the command line, by simply typing python.
What this does under Windows, is to trawl the %PATH% environment variable, checking for an executable, either batch file (.bat), command file (.cmd) or some other executable to run (this is controlled by the PATHEXT environment variable), that matches the name given. When it finds the correct file to run the file is being run.
Now, if you've installed two python versions 2.5 and 2.6, the path will have both of their directories in it, something like PATH=c:\python\2.5;c:\python\2.6 but Windows will stop examining the path when it finds a match.
What you really need to do is to explicitly call one or both of the applications, such as c:\python\2.5\python.exe or c:\python\2.6\python.exe.
The other alternative is to create a shortcut to the respective python.exe calling one of them python25 and the other python26; you can then simply run python25 on your command line.
Adding two more solutions to the problem:
Use pylauncher (if you have Python 3.3 or newer there's no need to install it as it comes with Python already) and either add shebang lines to your scripts;
#! c:\[path to Python 2.5]\python.exe - for scripts you want to be run with Python 2.5
#! c:\[path to Python 2.6]\python.exe - for scripts you want to be run with Python 2.6
or instead of running python command run pylauncher command (py) specyfing which version of Python you want;
py -2.6 – version 2.6
py -2 – latest installed version 2.x
py -3.4 – version 3.4
py -3 – latest installed version 3.x
Install virtualenv and create two virtualenvs;
virtualenv -p c:\[path to Python 2.5]\python.exe [path where you want to have virtualenv using Python 2.5 created]\[name of virtualenv]
virtualenv -p c:\[path to Python 2.6]\python.exe [path where you want to have virtualenv using Python 2.6 created]\[name of virtualenv]
for example
virtualenv -p c:\python2.5\python.exe c:\venvs\2.5
virtualenv -p c:\python2.6\python.exe c:\venvs\2.6
then you can activate the first and work with Python 2.5 like this
c:\venvs\2.5\activate
and when you want to switch to Python 2.6 you do
deactivate
c:\venvs\2.6\activate
From Python 3.3 on, there is the official Python launcher for Windows (http://www.python.org/dev/peps/pep-0397/). Now, you can use the #!pythonX to determine the wanted version of the interpreter also on Windows. See more details in my another comment or read the PEP 397.
Summary: The py script.py launches the Python version stated in #! or Python 2 if #! is missing. The py -3 script.py launches the Python 3.
As per #alexander you can make a set of symbolic links like below. Put them somewhere which is included in your path so they can be easily invoked
> cd c:\bin
> mklink python25.exe c:\python25\python.exe
> mklink python26.exe c:\python26\python.exe
As long as c:\bin or where ever you placed them in is in your path you can now go
> python25
For example for 3.6 version type py -3.6.
If you have also 32bit and 64bit versions, you can just type py -3.6-64 or py -3.6-32.
install python
C:\Python27
C:\Python36
environment variable
PYTHON2_HOME: C:\Python27
PYTHON3_HOME: C:\Python36
Path: %PYTHON2_HOME%;%PYTHON2_HOME%\Scripts;%PYTHON3_HOME%;%PYTHON3_HOME%\Scripts;
file rename
C:\Python27\python.exe → C:\Python27\python2.exe
C:\Python36\python.exe → C:\Python36\python3.exe
pip
python2 -m pip install package
python3 -m pip install package
I strongly recommend the pyenv-win project.
Thanks to kirankotari's work, now we have a Windows version of pyenv.
One easy way for this is that you can use
py -3.8 -m pip install virtualenv here -3.8 goes with your [version number]
After installing the virtualenv, you can create the virtual environment of your application using
py -3.8 -m virtualenv [your env name]
then cd to venv, enter activate
This would activate the python version you like.
Just change the version number to use a different python version.
When you install Python, it will not overwrite other installs of other major versions. So installing Python 2.5.x will not overwrite Python 2.6.x, although installing 2.6.6 will overwrite 2.6.5.
So you can just install it. Then you call the Python version you want. For example:
C:\Python2.5\Python.exe
for Python 2.5 on windows and
C:\Python2.6\Python.exe
for Python 2.6 on windows, or
/usr/local/bin/python-2.5
or
/usr/local/bin/python-2.6
on Windows Unix (including Linux and OS X).
When you install on Unix (including Linux and OS X) you will get a generic python command installed, which will be the last one you installed. This is mostly not a problem as most scripts will explicitly call /usr/local/bin/python2.5 or something just to protect against that. But if you don't want to do that, and you probably don't you can install it like this:
./configure
make
sudo make altinstall
Note the "altinstall" that means it will install it, but it will not replace the python command.
On Windows you don't get a global python command as far as I know so that's not an issue.
Here's a quick hack:
Go to the directory of the version of python you want to run
Right click on python.exe
Select 'Create Shortcut'
Give that shortcut a name to call by( I use p27, p33 etc.)
Move that shortcut to your home directory(C:\Users\Your name)
Open a command prompt and enter name_of_your_shortcut.lnk(I use p27.lnk)
cp c:\python27\bin\python.exe as python2.7.exe
cp c:\python34\bin\python.exe as python3.4.exe
they are all in the system path, choose the version you want to run
C:\Users\username>python2.7
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\username>python3.4
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The easiest way to run multiple versions of python on windows is described below as follows:-
1)Download the latest versions of python from python.org/downloads by selecting the relevant version for your system.
2)Run the installer and select Add python 3.x to the path to set path automatically in python 3 (you just have to click the checkbox). For python 2 open up your python 2 installer, select whatever preferences you want but just remember to set Add python.exe to path to Will be installed on local hard drive, Now just click next and wait for the installer to finish.
3)When both the installations are complete. Right click on my computer--Go to properties--Select advanced system settings--Go to environment variables--Click on new under System variables and add a new system variable with variable name as PY_PYTHON and set this variable value to 3. Now click on OK and you should be done.
4)Now to test this open the command prompt. Once you are in there type python or py, It should open up python3.
5)Now exit out of python3 by typing exit(). Now type py -2 it should open python 2.
If none of this works then restart the computer and if the problem still persists then uninstall everything and repeat the steps.
Thanks.
This is a simple and elegant solution to easily run 2 or more different versions of python without using scripts in Windows. Whatever the version of python, it will start from the Command prompt.
I have python versions 3.6.6 and 3.9. The Environment Variable paths are normal and were automatically added when each version of python was installed.
It's best to install python using the "all users" option. This way the python will simply install to:
C:\program files\python36
C:\program files\python39
Open each of these python folders and find the python.exe file. Copy and paste the python.exe file into those same folders. Then carefully rename the copies to:
python36.exe
python39.exe
Open and edit Environment Variables. Add 4 new User Variables.
C:\Program Files\Python36\Scripts
C:\Program Files\Python36\python36.exe
C:\Program Files\Python39\Scripts
C:\Program Files\Program39\python39.exe
Save and exit Environment Variables.
Open a new Command Prompt terminal window. To run one or the other version of python, type:
python36
python39
More versions of python can easily be added by repeating the same as shown above. Elegant and simple. Done.
Using a batch file to switch, easy and efficient on windows 7. I use this:
In the environment variable dialog (C:\Windows\System32\SystemPropertiesAdvanced.exe),
In the section user variables
added %pathpython% to the path environment variable
removed any references to python pathes
In the section system variables
removed any references to python pathes
I created batch files for every python installation (exmple for 3.4 x64
Name = SetPathPython34x64 !!! ToExecuteAsAdmin.bat ;-) just to remember.
Content of the file =
Set PathPython=C:\Python36AMD64\Scripts\;C:\Python36AMD64\;C:\Tcl\bin
setx PathPython %PathPython%
To switch between versions, I execute the batch file in admin mode.
!!!!! The changes are effective for the SUBSEQUENT command prompt windows OPENED. !!!
So I have exact control on it.
let's say if we have python 3.7 and python 3.6 installed.
they are respectively stored in following folder by default.
C:\Users\name\AppData\Local\Programs\Python\Python36
C:\Users\name\AppData\Local\Programs\Python\Python37
if we want to use cmd prompt to install/run command in any of the above specific environment do this:
There should be python.exe in each of the above folder.
so when we try running any file for ex. (see image1) python hello.py. we call that respective python.exe. by default it picks lower version of file. (means in this case it will use from python 3.6 )
image
so if we want to run using python3.7. just change the .exe file name. for ex. if I change to python37.exe and i want to use python3.7 to run hello.py
I will use python37 hello.py . or if i want to use python3.7 by default i will change the python.exe filename in python3.6 folder to something else . so that it will use python3.7 each time when I use only python hello.py
Shows your installed pythons
py -0
Uses version of python to do something
py -*version*
ex.
py -3.8 venv venv
Will create virtual environment in python 3.8
Note:
python -0
or
python -3.8
doesn't work, I assume it has to be "py"
You can create different python development environments graphically from Anaconda Navigator.
I had same problem while working with different python versions so I used anaconda navigator to create different python development environments and used different python versions in each environments.
Here is the help documentation for this.
https://docs.anaconda.com/anaconda/navigator/tutorials/manage-environments/
Introduce more details based on the answer given by #Aman.
Define different environment variables for different python versions.
For example:
You have E:\python2\python.exe and E:\python3\python.exe at the same time.
Then you can set an environment variable %python2% for E:\python2\python.exe and %python2% for E:\python3\python.exe.
Finally, when you want to run python2 (or python3), you can enter %python2% (or %python3%) directly in command prompt.
Here is a solution:
First, install all versions which you want to run in your pc. https://www.python.org/
Second, create virtual environment with which python version you want to use.
"py [python_version] -m venv [vritual_environment_name]" example: "py -3.9 -m venv env"
Note: You don't need to run "pip install virtualenv"
Using the Rapid Environment Editor you can push to the top the directory of the desired Python installation. For example, to start python from the c:\Python27 directory, ensure that c:\Python27 directory is before or on top of the c:\Python36 directory in the Path environment variable. From my experience, the first python executable found in the Path environment is being executed. For example, I have MSYS2 installed with Python27 and since I've added C:\MSYS2 to the path before C:\Python36, the python.exe from the C:\MSYS2.... folder is being executed.
I thought this answer might be helpful to others having multiple versions of python and wants to use pipenv to create virtual environment.
navigate to the project directory, and run py -[python version] pip install pipenv, example: py -3.6 pip install pipenv
run pipenv --python [version] to create the virtual environment in the version of the python you desire. example: pipenv --python 3.6
run pipenv shell to activate your virtual environment.
Just call the correct executable