Skip to content

Instantly share code, notes, and snippets.

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

@eschipul
Copy link

@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

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

@carpincho
Copy link

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

Thank you, this worked for me.

@SanthoshBabuMR
Copy link

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

Thanks, it worked perfectly!

@jeffshek
Copy link

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

@pacocontrerasdct
Copy link

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

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

@PayelKarmakar06
Copy link

@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

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

@vitalibertas
Copy link

@DsArshathR
Copy link

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

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

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

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)

@ziweiyang
Copy link

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!

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