I need to be able to switch back and forth between Python 2 and 3. How do I do that using Homebrew as I don't want to mess with path and get into trouble. Right now I have 2.7 installed through Homebrew.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I thought I had the same requirement - to move between Python versions - but I achieved all I needed with only Python3.6 by building from source instead of using
homebrew
.git clone https://git.<theThingYouWantToInstall>
Depending on the repo, check if there is MAKE file already setup for this option.
You can have both versions installed at the same time.
For Homebrew >=1.5.0:
Since 1st March 2018 the
python
formula will be upgraded to Python 3.x, while a newpython@2
formula will be added for Python 2.7, specifically.See changes announcement here or the final doc about using Homebrew for Python here.
For older Homebrew:
For Python 2.x:
For Python 3.x:
Now, you will have both the versions installed in your machine. When you want to use version 2, use the
python
executable. When you want to use version 3, use thepython3
executable.There are ways to use both , but the simplest solution today is to use pyenv. pyenv allows easy switching between versions. Here is what I did to set up:
STEP1:
Remove all pythons from your mac
Remove the following from
~/.bash_profile
and also the following from
~/.bashrc
STEP2:
Install pyenv and the python versions you need
STEP3:
add
pyenv init
tobash_profile
orbashrc
STEP4:
Check what got installed
STEP5:
Choose a default
When a project needs older version, just go its root folder and run
I would use pyenv You can install it:
To enable pyenv in your Bash shell, you need to run:
To do this automatically for Bash upon startup, add that line to your
~/.bash_profile
. 1Usage:
Once you have installed pyenv and activated it, you can install different versions of python and choose which one you can use. Example:
You can check the versions you have installed with:
And you can switch between python versions with the command:
Also you can set a python version for the current directory with:
You can check by running
python --version
:1 Homebrew used to instruct you to do this upon installation of pyenv, but the message was removed. For Zsh and other shells, the precise steps may be different.
Use asdf !
https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins
Currently Homebrew provides two different formulas for Python 2 and 3.
brew install python
installs python3, andbrew install python@2
installs python2. More details in Homebrew docs:https://docs.brew.sh/Homebrew-and-Python
If you currently have 2.x installed via Homebrew, Homebrew will give you a message such as:
If you run:
you should be able to do:
and
To see what versions of Python 2.x and 3.x installed.