Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save pandafulmanda/730a9355e088a9970b18275cb9eadef3 to your computer and use it in GitHub Desktop.

Select an option

Save pandafulmanda/730a9355e088a9970b18275cb9eadef3 to your computer and use it in GitHub Desktop.
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

tjt263 commented Mar 13, 2018

Copy link
Copy Markdown

@nkadel-placester: It doesn't.

@tjt263

tjt263 commented Mar 13, 2018

Copy link
Copy Markdown

@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
Copy Markdown

@tjt263 thank you, worked like a charm!

@kayyum1905

Copy link
Copy Markdown

@tjt263 thanks!

@enewhuis

enewhuis commented Apr 13, 2018

Copy link
Copy Markdown

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

rlam3 commented Apr 16, 2018

Copy link
Copy Markdown

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

@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

yernandus commented Dec 13, 2018

Copy link
Copy Markdown

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

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

@carpincho

Copy link
Copy Markdown

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

This actually worked for me! thanks!

@ical10

ical10 commented Apr 5, 2019

Copy link
Copy Markdown

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

Thank you, this worked for me.

@SanthoshBabuMR

Copy link
Copy Markdown

Thank you. It works!

@vitalibertas

vitalibertas commented Sep 18, 2019

Copy link
Copy Markdown

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

Thanks, it worked perfectly!

@jeffshek

Copy link
Copy Markdown

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

@pacocontrerasdct

Copy link
Copy Markdown

Great and simple! Thank you!

@llipe

llipe commented May 30, 2020

Copy link
Copy Markdown

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

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

@PayelKarmakar06

Copy link
Copy Markdown

@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
Copy Markdown

Youre awesomeeeee

@elulcao

elulcao commented Aug 23, 2020

Copy link
Copy Markdown

Worked like a charm!

@Swap76

Swap76 commented Nov 3, 2020

Copy link
Copy Markdown

Thanks It worked

@vitalibertas

Copy link
Copy Markdown

@DsArshathR

Copy link
Copy Markdown

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

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

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

@yeckfam

yeckfam commented Jul 12, 2022

Copy link
Copy Markdown

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)

@ziweiyang

Copy link
Copy Markdown

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

Thanks so much! Only this command gets worked for my Mac!

@yinjue

yinjue commented Jul 19, 2024

Copy link
Copy Markdown

Thanks
It worked for me too
python3 -m virtualenv <desired_path>

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