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 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 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 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 commented Mar 13, 2018

@nkadel-placester: It doesn't.

@tjt263

This comment has been minimized.

Copy link

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 commented Mar 19, 2018

@tjt263 thank you, worked like a charm!

@kayyum1905

This comment has been minimized.

Copy link

kayyum1905 commented Apr 8, 2018

@tjt263 thanks!

@enewhuis

This comment has been minimized.

Copy link

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 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 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 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 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 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 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 commented Jun 26, 2019

Thank you, this worked for me.

@SanthoshBabuMR

This comment has been minimized.

Copy link

SanthoshBabuMR commented Jul 23, 2019

Thank you. It works!

@vitalibertas

This comment has been minimized.

Copy link

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 commented Oct 21, 2019

Thanks, it worked perfectly!

@jeffshek

This comment has been minimized.

Copy link

jeffshek commented Nov 22, 2019

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

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.