Mac OS X comes with Python 2.7 out of the box.
You do not need to install or configure anything else to use Python 2. Theseinstructions document the installation of Python 3.
Python-magic is a Python interface to the libmagic file type identification library. Libmagic identifies file types by checking their headers according to a predefined list of file types. This functionality is exposed to the command line by the Unix command file. Virtualenv must be installed separately but supports Python 2.7+ and Python 3.3+ Pipenv is a higher-level tool that automatically manages a separate virtual environment for each project On MacOS we can install Pipenv with Homebrew.
The version of Python that ships with OS X is great for learning, but it’s notgood for development. The version shipped with OS X may be out of date from theofficial current Python release,which is considered the stable production version.
Doing it Right¶
Let’s install a real version of Python.
Before installing Python, you’ll need to install GCC. GCC can be obtainedby downloading Xcode, the smallerCommand Line Tools (must have anApple account) or the even smaller OSX-GCC-Installerpackage.
Python has been installed as. $ (brew -prefix)/opt/python@3.8/bin/python3. Unversioned symlinks `python`, `python-config`, `pip` etc. `python3`, `python3-config`, `pip3` etc., respectively, have been installed into. $ (brew -prefix)/opt/python@3.8/libexec/bin. You can install Python packages with. Pyjs Desktop allows the exact same Python web application source code to be executed as a standalone desktop application (running under Python) instead of being stuck in a Web browser. For Free Software platforms, most Linux Distributions now come with pyjs and pyjs Desktop pre-packaged: Debian/Testing, FC13, ArchLinux and Gentoo.
Note
If you already have Xcode installed, do not install OSX-GCC-Installer.In combination, the software can cause issues that are difficult todiagnose.
Note
If you perform a fresh install of Xcode, you will also need to add thecommandline tools by running xcode-select--install
on the terminal.
While OS X comes with a large number of Unix utilities, those familiar withLinux systems will notice one key component missing: a package manager.Homebrew fills this void.
To install Homebrew, open Terminal
oryour favorite OS X terminal emulator and run
The script will explain what changes it will make and prompt you before theinstallation begins.Once you’ve installed Homebrew, insert the Homebrew directory at the topof your PATH
environment variable. You can do this by adding the followingline at the bottom of your ~/.profile
file
If you have OS X 10.12 (Sierra) or older use this line instead
Now, we can install Python 3:
This will take a minute or two.
Pip¶
Homebrew installs pip
pointing to the Homebrew’d Python 3 for you.
Working with Python 3¶
At this point, you have the system Python 2.7 available, potentially theHomebrew version of Python 2 installed, and the Homebrewversion of Python 3 as well.
will launch the Homebrew-installed Python 3 interpreter.
will launch the Homebrew-installed Python 2 interpreter (if any).
will launch the Homebrew-installed Python 3 interpreter.
If the Homebrew version of Python 2 is installed then pip2
will point to Python 2.If the Homebrew version of Python 3 is installed then pip
will point to Python 3.
The rest of the guide will assume that python
references Python 3.
Pipenv & Virtual Environments¶
The next step is to install Pipenv, so you can install dependencies and manage virtual environments.
A Virtual Environment is a tool to keep the dependencies required by different projectsin separate places, by creating virtual Python environments for them. It solves the“Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keepsyour global site-packages directory clean and manageable.
For example, you can work on a project which requires Django 1.10 while alsomaintaining a project which requires Django 1.8.
So, onward! To the Pipenv & Virtual Environments docs!
This page is a remixed version of another guide,which is available under the same license.
Question or issue on macOS:
I just switched from MacPorts to HomeBrew. After installing all the required XCode versions and other software I tried installing python using homebrew: I think it successfully installed, but when I do which python it still shows me 2.7.3 which I think is the version shipped with Mountain Lion.
so I tried to install again
But it says python 2.7.5 already install and not linked, I tried to do brew link python
That led me to following message so, I have no idea what I should be doing:
Linking /usr/local/Cellar/python/2.7.5… Warning: Could not link python. Unlinking…
How to solve this problem?
Solution no. 1:
After installing python3 with brew install python3
I was getting the error:
After typing brew link python3
the error was:
To solve the problem:
After this, I could open python3 by typing python3
👍
(From https://github.com/Homebrew/homebrew-core/issues/20985)
Solution no. 2:
In the Terminal, type:
Solution no. 3:
If you used
before ‘unlink’
you got
so do
and open a new terminal shell
Brew Python 2
Solution no. 4:
On OS X High Sierra, I had to do this:
credit to https://gist.github.com/irazasyed/7732946#gistcomment-2235469
I think it’s better than recursively chowning the /usr/local dir, but that may solve other problems 😉
Solution no. 5:
I think you have to be precise with which version you want to link with the command brew link python
like:
It will give you an error like that:
Linking /usr/local/Cellar/python3/3.5.2…
Error: Could not symlink bin/2to3-3.5
Target /usr/local/bin/2to3-3.5
already exists.
You may want to remove it:
To force the link and overwrite all conflicting files:
To list all files that would be deleted:
but you have to copy/paste the command to force the link which is:
I think that you must have the version (the newer) installed.
Brew Python 3.8
Solution no. 6:
brew switch to python3 by default, so if you want to still set python2 as default bin python, running:
Solution no. 7:
You can follow these steps.
Solution no. 8:
This answer is for upgrading Python 2.7.10 to Python 2.7.11 on Mac OS X El Capitan . On Terminal type:
After that type on Terminal
Solution no. 9:
The problem with me is that I have so many different versions of python, so it opens up a different python3.7 even after I did brew link. I did the following additional steps to make it default after linking
First, open up the document setting up the path of python
Then something like this shows up:
The thing here is that my Python for brew framework is not in the Library Folder!! So I changed the framework for python 3.7, which looks like follows in my system
Brew Python 3.7
Change and save the file. Restart the computer, and typing in python3.7
, I get the python I installed for brew.
Not sure if my case is applicable to everyone, but worth a try. Not sure if the framework path is the same for everyone, please made sure before trying out.
Brew Update Python
Solution no. 10:
I use these commands to solve it.