Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setting up and using Python3 Virtualenv on Mac

Python3 Virtualenv Setup

Requirements
  • Python 3
  • Pip 3
$ brew install python3

Pip3 is installed with Python3

Installation

To install virtualenv via pip run:

$ pip3 install virtualenv
Usage

Creation of virtualenv:

$ virtualenv -p python3 <desired-path>

Activate the virtualenv:

$ source <desired-path>/bin/activate

Deactivate the virtualenv:

$ deactivate

About Virtualenv

@datatalking

This comment has been minimized.

Copy link

@datatalking 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

@nkadel-placester

This comment has been minimized.

Copy link

@nkadel-placester 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.

@raitisd

This comment has been minimized.

Copy link

@raitisd 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?

$ brew install python3
Error: python 2.7.13 is already installed
To upgrade to 3.6.4_3, run `brew upgrade python`
@tjt263

This comment has been minimized.

Copy link

@tjt263 tjt263 commented Mar 13, 2018

@nkadel-placester: It doesn't.

@tjt263

This comment has been minimized.

Copy link

@tjt263 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:

brew uninstall --force --ignore-dependencies python python2 python2.7 python3 python3.6 > /dev/null 2>&1
brew install python@2 python@3 > /dev/null 2>&1
echo; for x in python python2 python3; do 
which $x; readlink $(which $x); $x --version; echo; done

The output should look like this:

/usr/local/bin/python
../Cellar/python@2/2.7.14_3/bin/python
Python 2.7.14

/usr/local/bin/python2
../Cellar/python@2/2.7.14_3/bin/python2
Python 2.7.14

/usr/local/bin/python3
../Cellar/python/3.6.4_4/bin/python3
Python 3.6.4

If it does, it should be fixed.

The copy of python that belongs to the OS still lives here: /usr/bin/python

@MassimoDIFA

This comment has been minimized.

Copy link

@MassimoDIFA MassimoDIFA commented Mar 19, 2018

@tjt263 thank you, worked like a charm!

@kayyum1905

This comment has been minimized.

Copy link

@kayyum1905 kayyum1905 commented Apr 8, 2018

@tjt263 thanks!

@enewhuis

This comment has been minimized.

Copy link

@enewhuis enewhuis commented Apr 13, 2018

Well I tried this and it doesn't work for me. It fails here:

$ virtualenv -p python3 venv
-bash: virtualenv: command not found

$ pip3 install virtualenv
Requirement already satisfied: virtualenv in /Users/enewhuis/Library/Python/3.6/lib/python/site-packages

I can add /Users/enewhuis/Library/Python/3.6/bin to my path but, as is, the instructions here didn't work.

@rlam3

This comment has been minimized.

Copy link

@rlam3 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!

@eschipul

This comment has been minimized.

Copy link

@eschipul 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.

@yernandus

This comment has been minimized.

Copy link

@yernandus 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

solved it

@nathaniel-lemonade

This comment has been minimized.

Copy link

@nathaniel-lemonade nathaniel-lemonade commented Jan 14, 2019

if it fails for you in:
virtualenv -p python3 <desired_path>
Try:
python3 -m virtualenv <desired_path>

@carpincho

This comment has been minimized.

Copy link

@carpincho carpincho commented Mar 23, 2019

if it fails for you in:
virtualenv -p python3 <desired_path>
Try:
python3 -m virtualenv <desired_path>

This actually worked for me! thanks!

@ical10

This comment has been minimized.

Copy link

@ical10 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 <name-of-venv>

@Coder2100

This comment has been minimized.

Copy link

@Coder2100 Coder2100 commented Jun 26, 2019

Thank you, this worked for me.

@SanthoshBabuMR

This comment has been minimized.

Copy link

@SanthoshBabuMR SanthoshBabuMR commented Jul 23, 2019

Thank you. It works!

@vitalibertas

This comment has been minimized.

Copy link

@vitalibertas 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.

@nabeel-shakeel

This comment has been minimized.

Copy link

@nabeel-shakeel nabeel-shakeel commented Oct 21, 2019

Thanks, it worked perfectly!

@jeffshek

This comment has been minimized.

Copy link

@jeffshek jeffshek commented Nov 22, 2019

Totally saved me after I had an issue with brew. Thank you!

@pacocontrerasdct

This comment has been minimized.

Copy link

@pacocontrerasdct pacocontrerasdct commented May 12, 2020

Great and simple! Thank you!

@llipe

This comment has been minimized.

Copy link

@llipe 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.

@qnicole-zakka

This comment has been minimized.

Copy link

@qnicole-zakka qnicole-zakka commented Jun 15, 2020

haven't used MacOS for a while. Saving this for the new laptop:)

@PayelKarmakar06

This comment has been minimized.

Copy link

@PayelKarmakar06 PayelKarmakar06 commented Jun 27, 2020

@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:

brew uninstall --force --ignore-dependencies python python2 python2.7 python3 python3.6 > /dev/null 2>&1
brew install python@2 python@3 > /dev/null 2>&1
echo; for x in python python2 python3; do 
which $x; readlink $(which $x); $x --version; echo; done

The output should look like this:

/usr/local/bin/python
../Cellar/python@2/2.7.14_3/bin/python
Python 2.7.14

/usr/local/bin/python2
../Cellar/python@2/2.7.14_3/bin/python2
Python 2.7.14

/usr/local/bin/python3
../Cellar/python/3.6.4_4/bin/python3
Python 3.6.4

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'

AN ERROR OCCURRED

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 **

/usr/bin/python
Python 2.7.10

-bash: python2: command not found

-bash: python3: command not found
**

Any idea what could possibly be the issue?

@Alsaheem

This comment has been minimized.

Copy link

@Alsaheem Alsaheem commented Jul 26, 2020

Youre awesomeeeee

@elulcao

This comment has been minimized.

Copy link

@elulcao elulcao commented Aug 23, 2020

Worked like a charm!

@Swap76

This comment has been minimized.

Copy link

@Swap76 Swap76 commented Nov 3, 2020

Thanks It worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.