Marina Mele’s site
Learn about Python, Django, Marketing and Science
Install Python 3 on Mac OS X and use virtualenv and virtualenvwrapper
This post explains how to install Python 3 in a Mac OS X, both Mavericks and Yosemite. Check this post if you want to clean install Python 2.7. You can also install both! 🙂
It also shows how to use virtualenv and virtualenvwrapper with Python 3. Don’t miss it!
Like with Python 2.7, we need to install first Xcode and Homebrew.
Install Xcode and Homebrew
First of all, install Xcode if you don’t have it already. You can find it in the Apple Store.
Next, we need to install the Command Line Tools of Xcode. Open a Terminal and type:
this should trigger a pop-up window that will ask you to install the Command Line Tools. If you have some trouble installing these tools, you might find useful this post on Stackoverflow.
Next, we need to install Homebrew. In the Terminal, type this command line:
Now, we need to insert the Homebrew directory at the top of the PATH environment variable. In this way, some Homebrew installations will take precedence over stock OS X binaries. Open or create the file
/.bash_profile and write:
Close your Terminal and open it again to make these changes effective.
Install Python 3
you will see the available python-related packages to install, and python3 should be among them. Let’s install it!
You can check which version is installed by typing
And you can open it with:
Moreover, when you install python with Homebrew, you also install:
- the corresponding pip package manager, which is called pip3
- the corresponding Setuptools
- pyvenv, and alternative to virtualenv — cool!!
Create Virtual environments with pyvenv
Now that you have Python3 you also have pyvenv, a tool to create virtual environments (similar to virtualenv). However, there is one important remark about the version of pyvenv you have installed: only if you installed Python 3.4 or latter, pyvenv will also install pip when creating a new virtual environment.
Let’s create a new virtual envirnoment, named myenv, using pyvenv:
pandafulmanda / Python3 Virtualenv Setup.md
Python3 Virtualenv Setup
Pip3 is installed with Python3
To install virtualenv via pip run:
Creation of virtualenv:
Activate the virtualenv:
Deactivate the virtualenv:
This comment has been minimized.
Copy link Quote reply
datatalking commented Dec 28, 2017
I’ve tried three virtual env tutorials and this is the only one that worked the first time.
Thanks for posting, you get a star
This comment has been minimized.
Copy link Quote reply
nkadel-placester commented Feb 14, 2018
It requires a «github» python module. Which one? The «github» module at pypi.org has no content. I realize many python modules have confusing names, so assume you’re using a specific upstream module.
This comment has been minimized.
Copy link Quote reply
raitisd commented Mar 7, 2018
I get this error when trying to install python3. I would like to have python 2.7 as my default python and only use python3 in some virtual environments. Whats the deal here?
This comment has been minimized.
Copy link Quote reply
tjt263 commented Mar 13, 2018
This comment has been minimized.
Copy link Quote reply
tjt263 commented Mar 13, 2018
@raitisd: Well, that’s the idea.
For some reason, brew recently decided that python refers to python3 instead of python2 .
It’s a pain in the ass.
Here, I wrote you a little script:
The output should look like this:
If it does, it should be fixed.
The copy of python that belongs to the OS still lives here: /usr/bin/python
This comment has been minimized.
Copy link Quote reply
MassimoDIFA commented Mar 19, 2018
@tjt263 thank you, worked like a charm!
This comment has been minimized.
Copy link Quote reply
kayyum1905 commented Apr 8, 2018
This comment has been minimized.
Copy link Quote reply
enewhuis commented Apr 13, 2018 •
Well I tried this and it doesn’t work for me. It fails here:
I can add /Users/enewhuis/Library/Python/3.6/bin to my path but, as is, the instructions here didn’t work.
This comment has been minimized.
Copy link Quote reply
rlam3 commented Apr 16, 2018 •
This fails and doesn’t recognize python 3.
I need to have python 2.7 and python 3 on my system.
I’m currently using virtualenvwrapper with 2.7. Is there a way to integrate python 3 to take advantage of virtualenvwrapper?
Thanks!
This comment has been minimized.
Copy link Quote reply
eschipul commented Jul 12, 2018
@rlam3 — you can install python3 independently from python on a Mac or PC. While I’m not a huge fan of non-native development (meaning I recommend developing on linux with vmware if you are deploying to a linux variant), you can still install python side by side.
It’s would also be helpful if you mentioned your operating system, current file path, etc. People want to help but we need details.
There is a course on udemy (google it as «learn-python-3-from-beginner-to-advanced» and you’ll find it). I have no connection with them, but that course does a good job of getting you started with your development environment imho.
This comment has been minimized.
Copy link Quote reply
yernandus commented Dec 13, 2018 •
I had this problem Linking /usr/local/Cellar/python/3.7.1. Error: Permission denied @ dir_s_mkdir — /usr/local/Frameworks
and
sudo mkdir /usr/local/Frameworks
sudo chown $(whoami):admin /usr/local/Frameworks
This comment has been minimized.
Copy link Quote reply
nathaniel-lemonade commented Jan 14, 2019
if it fails for you in:
virtualenv -p python3
Try:
python3 -m virtualenv
This comment has been minimized.
Copy link Quote reply
carpincho commented Mar 23, 2019
if it fails for you in:
virtualenv -p python3
Try:
python3 -m virtualenv
This actually worked for me! thanks!
This comment has been minimized.
Copy link Quote reply
ical10 commented Apr 5, 2019
Anyone knows how to activate the virtualenv without having to remember the path to its directory? I’m talking about command similar to conda virtualenv.
conda activate
This comment has been minimized.
Copy link Quote reply
Coder2100 commented Jun 26, 2019
Thank you, this worked for me.
This comment has been minimized.
Copy link Quote reply
SanthoshBabuMR commented Jul 23, 2019
Thank you. It works!
This comment has been minimized.
Copy link Quote reply
vitalibertas commented Sep 18, 2019 •
Thanks! Every single time Homebrew does an update to Python I forget that the new version totally hoses up the venv (because I never deactivate it) so your page is very useful for me every couple of months.
This comment has been minimized.
Copy link Quote reply
nabeel-shakeel commented Oct 21, 2019
Thanks, it worked perfectly!
This comment has been minimized.
Copy link Quote reply
jeffshek commented Nov 22, 2019
Totally saved me after I had an issue with brew. Thank you!
This comment has been minimized.
Copy link Quote reply
pacocontrerasdct commented May 12, 2020
Great and simple! Thank you!
This comment has been minimized.
Copy link Quote reply
llipe commented May 30, 2020
Awesome, I needed to steal it as a Gist for future references. Thanks.
As a note, is common to use «venv» as the path/folder to store virtual env info. This is usually included within .gitignore files.
This comment has been minimized.
Copy link Quote reply
qnicole-zakka commented Jun 15, 2020
haven’t used MacOS for a while. Saving this for the new laptop:)
This comment has been minimized.
Copy link Quote reply
PayelKarmakar06 commented Jun 27, 2020
For some reason, brew recently decided that python refers to python3 instead of python2 .
It’s a pain in the ass.
Here, I wrote you a little script:
The output should look like this:
If it does, it should be fixed.
The copy of python that belongs to the OS still lives here: /usr/bin/python
I was getting this error **
RuntimeError: failed to find interpreter for Builtin discover of python_spec=’python3′
java.lang.IllegalStateException: Creating virtual env exited with 1. Check the logs for more details.
**
I am using MacOS, and python2.7 comes inbuilt. I was facing some issues while installing virtualenv, installed it with pip after some efforts. But now I think the conflict is due to the python versions (2.7 & 3). I do not have Python 3 installed in my system.
So I tried your script @tjt263, but got the below response **
Install virtualenv and virtualenvwrapper on MacOS
How to install and configure virtualenv and virtualenvwrapper for Python on OSX ?
2 Answers 2
The best way to install virtualenv and virtualenvwrapper is on top of correctly configured python (in this example I’ll be using python 3 but process is identical for python 2 ).
In OSX the most convenient and correct way to install python is using brew . Main difference between installing python with installer and brew is that installer puts python packages to:
Brew on the other hand installs Python , Pip & Setuptools and puts everything to:
1. Install brew
Check out brew installation page or simply run this in your terminal:
2. Install Python
To install python with brew run:
Now your system needs to know where to look for freshly installed Python packages. Add this line to youre
/.bash_profile if you’re using bash ):
Restart your terminal. To make sure you’ve done everything correctly run which python3 , you should receive /usr/local/bin/python .
3. Install virtualenv & virtualenvwrapper
Now it’s time to install virtualenv and virtualenvwrapper to be able to use workon command and switch between environments. This is done using pip :
4. Set up virtualenv variables
Your need to define path where to keep your virtual environments. In my case I’ve created a hidden directory inside
and called it .virtualenvs with mkdir
/.virtualenvs . Add virtualenv variables to .zshrc (or .bash_profile ).
Final version of your .zshrc (or .bash_profile ) should contain this information to work properly with installed packages:
Restart your terminal. You should be able to use mkvirtualenv and workon commands including autocompletion.
Here’s a little tip on how to create virtualenv with specific version of Python.
In case you are using MacOS Mojave and you are installing Python3.6 from brew bottle you might have a problem with pip , here’s a solution that might help.