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

@tjt263
Copy link

tjt263 commented Mar 13, 2018

@nkadel-placester: It doesn't.

@tjt263
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
Copy link

MassimoDIFA commented Mar 19, 2018

@tjt263 thank you, worked like a charm!

@kayyum1905
Copy link

kayyum1905 commented Apr 8, 2018

@tjt263 thanks!

@enewhuis
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
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
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
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
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
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
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
Copy link

Coder2100 commented Jun 26, 2019

Thank you, this worked for me.

@SanthoshBabuMR
Copy link

SanthoshBabuMR commented Jul 23, 2019

Thank you. It works!

@vitalibertas
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
Copy link

nabeel-shakeel commented Oct 21, 2019

Thanks, it worked perfectly!

@jeffshek
Copy link

jeffshek commented Nov 22, 2019

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

@pacocontrerasdct
Copy link

pacocontrerasdct commented May 12, 2020

Great and simple! Thank you!

@llipe
Copy link

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
Copy link

qnicole-zakka commented Jun 15, 2020

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

@PayelKarmakar06
Copy link

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
Copy link

Alsaheem commented Jul 26, 2020

Youre awesomeeeee

@elulcao
Copy link

elulcao commented Aug 23, 2020

Worked like a charm!

@Swap76
Copy link

Swap76 commented Nov 3, 2020

Thanks It worked

@armagangok
Copy link

armagangok commented Feb 6, 2021

Hello! How can upgrade a module in venv? for example tkinter

@vitalibertas
Copy link

vitalibertas commented Feb 6, 2021

@DsArshathR
Copy link

DsArshathR commented Jul 10, 2021

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

If i use both of this i get this error
Someone help me rectify it
zsh: parse error near `\n'

@evanrianto
Copy link

evanrianto commented Nov 22, 2021

Hi, what if my macOS already has python3 and if I installed python3 via brew, its not going to use the brew version. What should I do ? Thanks!

@NuraiymS
Copy link

NuraiymS commented Nov 30, 2021

hello all,
I have the same error:

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

If i use both of this i get this error
Someone help me rectify it
zsh: parse error near `\n'

can someone help me please ???

@HamitMashot
Copy link

HamitMashot commented Dec 15, 2021

Creation of virtualenv:
python3 -m venv <desired_path>

To create a virtual environment, go to your project’s directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands.

@yeckfam
Copy link

yeckfam commented Jul 12, 2022

Got below error after enter: pip3 install virtualenv, how do I resolve this?

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: virtualenv in /Users/fanyang/Library/Python/3.8/lib/python/site-packages (20.15.1)
Requirement already satisfied: distlib<1,>=0.3.1 in /Users/fanyang/Library/Python/3.8/lib/python/site-packages (from virtualenv) (0.3.4)
Requirement already satisfied: six<2,>=1.9.0 in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages (from virtualenv) (1.15.0)
Requirement already satisfied: platformdirs<3,>=2 in /Users/fanyang/Library/Python/3.8/lib/python/site-packages (from virtualenv) (2.5.2)
Requirement already satisfied: filelock<4,>=3.2 in /Users/fanyang/Library/Python/3.8/lib/python/site-packages (from virtualenv) (3.7.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment