Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install virtualenv and virtualenvwrapper on MacOS

How to install and configure virtualenv and virtualenvwrapper for Python on MacOS?

like image 609
Stan Redoute Avatar asked Mar 24 '18 21:03

Stan Redoute


People also ask

Where is virtualenv installed on Mac?

It will get installed in the venv/ folder, and not conflict with other projects.

What is the difference between virtualenv and virtualenvwrapper?

Virtualenvwrapper is a utility on top of virtualenv that adds a bunch of utilities that allow the environment folders to be created at a single place, instead of spreading around everywhere.

Where is virtualenvwrapper installed?

That installation installs virtualenvwrapper in the /usr/local/bin directory.

How to install virtualenv and virtualenv wrapper on macOS?

To install virtualenv and virtualenvwrapper for repetitive use you need a correctly configured Python (this example uses Python 3.x but process is identical for Python 2.x ). Although you can get python installer from Python website I strongly advice against it. The most convenient and future-proof method to install Python on MacOS is brew.

How to install Python and virtualenv on macOS?

Installing Python and virtualenv on MacOS 1 Requirements. To follow these instructions you need to at least have installed brew on MacOS. ... 2 Installing Python 3.7.x and Python 2.7.x. ... 3 Install virtualenv and virtualenvwrapper. ... 4 Test if the installed tools are working. ... 5 Conclusion. ...

What can be installed in a virtualenv?

All the rest can be installed in a virtualenv. Virtualenvwrapper To make it easier to work on multiple projects that has separate environments you can install virtualenvwrapper. It's an extension to virtualenvand makes it easier to create and delete virtual environments without creating dependency conflicts.

How do I install virtualenv wrapper in Pip?

To install virtualenvwrapperrun: pip install virtualenvwrapper Depending on your setup you might need to install it using sudo. Read the installation documentationfor more information. Note: virtualenvwrapperkeeps all the virtual environments in ~/.virtualenvwhile virtualenvkeeps them in the project directory.


2 Answers

To install virtualenv and virtualenvwrapper for repetitive use you need a correctly configured Python (this example uses Python 3.x but process is identical for Python 2.x).

Although you can get python installer from Python website I strongly advice against it. The most convenient and future-proof method to install Python on MacOS is brew.

Main difference between installer from Python website and brew is that installer puts python packages to:

/Library/Frameworks/Python.framework/Versions/3.x 

Brew on the other hand installs Python, Pip & Setuptools and puts everything to:

/usr/local/bin/python3.x/site-packages 

And though it may not make any difference to you now – it will later on.

Configuration steps

  1. Install brew

Check out brew installation page or simply run this in your terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 
  1. Install Python

To install python with brew run:

brew install python3 

Now your system needs to know where to look for freshly installed Python packages. Add this line to youre ~/.zshrc (or ~/.bash_profile if you're using bash):

export PATH=/usr/local/share/python:$PATH 

Restart your terminal. To make sure you've done everything correctly run which python3 and in return you should receive /usr/local/bin/python.

  1. Install virtualenv & virtualenvwrapper

Now it's time to install virtualenv and virtualenvwrapper to be able to use workon command and switch between virtual environments. This is done using pip:

pip3 install virtualenv virtualenvwrapper 
  1. Set up virtualenv variables

Define a default path for your virtual environments. For example you can create a hidden directory inside ~ and called it .virtualenvs with mkdir ~/.virtualenvs. Add virtualenv variables to .zshrc (or .bash_profile).

Final version of your .zshrc (or .bash_profile) should contain this information to work properly with installed packages:

# Setting PATH for Python 3 installed by brew export PATH=/usr/local/share/python:$PATH  # Configuration for virtualenv export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv source /usr/local/bin/virtualenvwrapper.sh 

Restart your terminal. You should be able to use mkvirtualenv and workon commands including autocompletion.

Here's a little tip on how to create virtualenv with specific version of Python.

In case you are using MacOS Mojave and you are installing Python3.6 from brew bottle you might have a problem with pip, here's a solution that might help.


With time some of you may want to install multiple Python versions with multiple virtual environments per version. When this moment comes I strongly recommend swithing to pyenv and pyenv-virtualenv .

like image 109
Stan Redoute Avatar answered Sep 25 '22 12:09

Stan Redoute


I'm running macOS 10.15.7

I followed official docs until here

and change it to

export WORKON_HOME=$HOME/.virtualenvs  export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3  # export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv export VIRTUALENVWRAPPER_VIRTUALENV=/Library/Frameworks/Python.framework/Versions/3.8/bin/virtualenv  # source /usr/local/bin/virtualenvwrapper.sh source /Library/Frameworks/Python.framework/Versions/3.8/bin/virtualenvwrapper.sh 

in your case try to run which virtualenv or which python to get exact paths

like image 30
sultanmyrza Avatar answered Sep 23 '22 12:09

sultanmyrza