Install virtualenv and virtualenvwrapper on MacOS - python-3.x

How to install and configure virtualenv and virtualenvwrapper for Python 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.
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
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)"
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.
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
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 .

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

Mac Big Sur
Python 3.8
installation
pip3 install virtualenv virtualenvwrapper
or
pip3 install virtualenv virtualenvwrapper --user
create directoty in your Home
mkdir .virtualenvs
edit profile
vi .bash_profile
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV=/Users/{your_user}/Library/Python/3.8/bin/virtualenv
source /Users/{home_directory}/Library/Python/3.8/bin/virtualenvwrapper.sh
check path with 'which' command or 'find':
Reload .bash_profile
source ~/.bash_profile

If you are using MacOS
1.
Install virtualenvwrapper
pip3 install virtualenvwrapper
2.
Create the "virtualenvs" folder
mkdir ~/.virtualenvs
3.
Before you export this, make sure you are doing it in your project folder,
because that's where you will activate the virtualenv
Export
export WORKON_HOME=$HOME/.virtualenvs
next, write down in terminal which python3, to find the path and add it after the "=" sign
export VIRTUALENVWRAPPER_PYTHON=
in my case:
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
to find the virtualenv path write in the terminal which virtualenv
add the path after the "=" sign
export VIRTUALENVWRAPPER_VIRTUALENV=
in my case:
export VIRTUALENVWRAPPER_VIRTUALENV=/Library/Frameworks/Python.framework/Versions/3.9/bin/virtualenv
4.
Last step is to add the source in the terminal
almost the same as the path you got when writing which virtualenv
The difference is the "wrapper.sh" in the end.
in my case
source /Library/Frameworks/Python.framework/Versions/3.9/bin/virtualenvwrapper.sh
5.
Now you can create the virtualenv name by doing the following:
mkvirtualenv nameOfTheVirtualEnviroment
in my case
mkvirtualenv venv
On this Mozila page you can learn how to use it

I had many problems to install the virtualenvwrapper in my mac os (big sur) but after I spent my whole sunday on that I finally got it. What I did I do:
I reinstalled python3 using the homebrew. For some reason homebrew
put your files in a non conventional directories (for exemple:
/opt/homebrew/bin/python3 or /opt/homebrew/bin/virtualenv)
Than I installed the virtualenv (even its already installed in python3
by default) and I installed virtualenvwrapper in sudo mode: sudo pip3
install virtualenvwrapper (without sudo, I could't find the
virtualenvwrapper.sh using whichvirtualenvwrapper).
After that, in my home directory (/˜), I created the .bashrc file using
touch .bashrc because this file didn't existed in my home directory.
I used the command "which python3", "which virtualenv" and "which
virtualenvwrapper.sh" to get the localization of them (it's important to
get these location to fill the .bashrc file later). The result was:
/opt/homebrew/bin/python3
/opt/homebrew/bin/virtualenv
/opt/homebrew/bin/virtualenvwrapper.sh
Than, using vim editor, I wrote in this file like this:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_PYTHON=/opt/homebrew/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/opt/homebrew/bin/virtualenv
source /opt/homebrew/bin/virtualenvwrapper.sh
Than, I saved this file using :wq command and run it using this command:
source .bashrc
Than I testes using the writing "workon" command. It showed nothing so
it worked
Finally I create a virtual environment. It worked.
I made a lot of mistakes and I really hope I did't damage my OS.

The following solution worked for me. I have checked for the python path using which python3.8 in my MAC laptop with OS Big Sur (11.4)
/Users/apple/opt/anaconda3/bin/python3.8
Also checked for which virtualenvwrapper.sh
/Users/apple/opt/anaconda3/bin/virtualenvwrapper.sh
Updated my ~/.bash_profile with the above mentioned paths as shown below.
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/Users/apple/opt/anaconda3/bin/python3.8
source /Users/apple/opt/anaconda3/bin/virtualenvwrapper.sh
Then I was successfully able to source ~/.bash_profile.

this solution also works for version 11.1
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

Stan's solution required different .bash_profile modification for me:
(macOS Catalina & Python 3.6) Please make sure you change the version path from "..../3.6/..." to your version. You can use "which python3" command to find out python path and "which virtualenv" for virtualenv path
### BEGIN: for virtualenvwrapper #########################
# Setting PATH for Python 3 installed by brew
# commented the Python path because I have already had that, you might have too.
# PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin/python3:${PATH}"
# export PATH
# Configuration for virtualenv
WORKON_HOME="${HOME}/.virtualenvs"
export WORKON_HOME
VIRTUALENVWRAPPER_PYTHON="/Library/Frameworks/Python.framework/Versions/3.6/bin/python3"
export VIRTUALENVWRAPPER_PYTHON
VIRTUALENVWRAPPER_VIRTUALENV="/Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenv"
export VIRTUALENVWRAPPER_VIRTUALENV
source /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh
### END: for virtualenvwrapper #########################

Related

Installed python packages will stay in site packages 3.10?

Specs:
Macbook Air M1 Ventura
Visual Studio Code Version: 1.75.0
which brew
/opt/homebrew/bin/brew
which pip3
/opt/homebrew/bin/pip3
(which is a link to:
/opt/homebrew/Cellar/python#3.10/3.10.10/bin/pip3 )
tl;dr:
I don't have python installed through brew anymore, but only as pyenv (and only version 3.11.1) - my Mac native version is 3.9.6)
YET whenever I install something through the activated virtualenv it will re-create a folder in /opt/homebrew/lib/python3.10/site-packages.
I don't understand why it keeps fussing about python3.10 so much and how I can get packages installed to any other environment. It seems my pc is only the folder mentioned above.
What I tried:
Since a module (twarc-csv as part of the twarc2 package) didn't work anymore (when trying to run, it says Error: No such command 'csv'.; which twarc-csv in terminal also didn't find it), I started looking into my python environments. the developer of the package suggested it was related to versionings and recommend using pyenv.
I have to say that before installing pyenv I upgraded twarc (to be sure) in terminal with brew install twarc --upgrade (as I already figured my pip3 path and my python path didn't align see python mess) which automatically fetched python3.11.1 as a dependency for twarc for some reason. As a result besides the Mac native python version I now had python 3.11 and 3.10 installed.
Even when I explicitly ran 3.11 though (e.g. python3.11 -m pip install twarc --upgrade twarc-csv), packages would still be installed to this folder /opt/homebrew/lib/python3.10/site-packages though. So something was off.
I decided to go with the suggestion from said developer and try pyenv. I strictly stuck to this guide: clean pyenv environment and VSCode seemed to find my pyenv virtualenv installed python version. However even though which pip3 led to the virtual shims path, the packages were still installed to /opt/homebrew/lib/python3.10/site-packages
Ok, if I wouldn't really need this package to run, I would already have given up, but since things are the way they are... I decided to delete pyenv and reinstall python3 through brew.
As you might have figured by now, the cycle started from anew. I wanted to check into why this path is so dear to my setup /opt/homebrew/lib/python3.10/site-packages Visual Studio would also still indicate that python 3.10.10 is global, so I checked
import sys
import pprint
pprint.pprint(sys.path)
I checked this for 3.11 and 3.10 environment in VSCode
the one for 3.10 being
'/opt/homebrew/Cellar/python#3.10/3.10.10/Frameworks/Python.framework/Versions/3.10/lib/python310.zip',
'/opt/homebrew/Cellar/python#3.10/3.10.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10',
'/opt/homebrew/Cellar/python#3.10/3.10.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload',
'/opt/homebrew/lib/python3.10/site-packages']
the one for 3.11 being
'/opt/homebrew/Cellar/python#3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python311.zip',
'/opt/homebrew/Cellar/python#3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11',
'/opt/homebrew/Cellar/python#3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload',
'/opt/homebrew/lib/python3.11/site-packages']
plus my desktop, so this seems to be fine. Nevertheless even if I use interpreter 3.11 in VSCode it will still look for packages in python3.10 and not install them to 3.11
Now I checked $PATH and see what it says, and it still refers to pyenv it seems:
$PATH
zsh: no such file or directory: /Users/myusername/.pyenv/shims:/Users/myusername/.pyenv/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:/Library/Apple/usr/bin
So how do I clean that up?
Also, how do I get things to run correctly? I do not understand that affection to the sitepackages 3.10 at all.
Update:
I am wondering whether my zprofile, zshrc or bashrc or bash_profile files have to do with it.
That's how they look:
(bashrc is empty)
bash_profile
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
zprofile
# Set PATH, MANPATH, etc., for Homebrew.
eval "$(/opt/homebrew/bin/brew shellenv)"
zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi

pip in path but command not found

I'm running a fresh installed OctoPi image (0.18.0). I added pip to my PATH in .bashrc file and sourced it but when I try to run pip I'm getting -bash: pip: command not found error.
When I run /home/pi/oprint/bin/pip, pip is working properly.
My path:
pi#octopi:~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/oprint/bin/pip:
You added the full path of the executable to the path, but you're only supposed to add the directory of the executable. Every exectuable of said directory is then directly available without the directory prefix.
In other words .. instead of /home/pi/oprint/bin/pip you should add /home/pi/oprint/bin/ to the path.
I always recommend using the Python executable to locate the pip executable for you. This way, you know the right version of pip is being used to install packages that are compliant with your Python version.
$ python -m pip install <the_package_you_want>
It is really easy to end up with mismatched versions otherwise!

No module named virtualenvwrapper?

I'm trying to install openCV on my new Mac (Mojave).So, I'm following this tutorial.
I want to create a new virtual whit this command:
mkvirtualenv cv3 -p python3
But sometimes I get the following output on my terminal:
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/keyone/code/.virtualenvs/cv3/bin/python3.7
Not overwriting existing python script /Users/keyone/code/.virtualenvs/cv3/bin/python (you must use /Users/keyone/code/.virtualenvs/cv3/bin/python3.7)
Installing setuptools, pip, wheel...
done.
/usr/bin/python: No module named virtualenvwrapper
or this output:
-bash: mkvirtualenv: command not found
This is how my .bash_profile file looks like:
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
export PATH=/usr/local/bin:$PATH
export WORKON_HOME=$HOME/code/.virtualenvs
export PROJECT_HOME=$HOME/code
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
I looked on stack overflow to find a solution, but nothing is working for me. virtualen, python etc. are already installed.
Try using virtualenv -p python3 envname as it work for me in mac book : D

Problems installing virtualenvwrapper in Cygwin

I'm trying to get started with a book called "TDD with Python", OS W10. As part of the setup it wants you to install virtualenvwrapper. The recommendation is to use GitBash in Windows... but I've been using Cygwin for quite a bit and didn't like GitBash when I looked at it some time ago.
Python version is 3.6. I installed virtualenvwrapper by going
pip install virtualenvwrapper
This seemed to work OK.
Trying to add the relevant setup line to my .bashrc did not go so well:
echo "source virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc
output:
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization
hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is set
properly.
At least this proves that virtualenvwrapper.sh is being run...
In fact there is no such directory as /usr/bin ... so I added a preceding line in .bashrc pointing to what I hoped might be what it might be wanting to see for this VIRTUALENVWRAPPER_PYTHON environment variable (do we call them that in Linux?).
VIRTUALENVWRAPPER_PYTHON=/cygdrive/d/apps/Python/Python36/Lib/site-packages/virtualenvwrapper
Now I get
bash:
/cygdrive/d/apps/Python/Python36/Lib/site-packages/virtualenvwrapper:
Is a directory virtualenvwrapper.sh: There was a problem running the
initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/cygdrive/d/apps/Python/Python36/Lib/site-packages/virtualenvwrapper
and that PATH is set properly.
Main question: what is VIRTUALENVWRAPPER_PYTHON meant to point at? An executable file? Any clues to help me recognise the desired target would be much appreciated!
POST-ANSWER ENLIGHTENMENT Just in case anyone stumbles this way, just to let you know: after phd gave me the answer to this I found other problems.
Eventually I managed to install virtualenvwrapper by doing this:
pip uninstall virtualenvwrapper
... use the latest Cygwin setup .exe to install pip3. Note the 3!
then go
pip3 install virtualenvwrapper
echo "source virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc
... success! but quickly followed by another puzzle: see here.
VIRTUALENVWRAPPER_PYTHON should point to python binary: /usr/bin/python, e.g.

virtualenv: cannot import name 'main'

I'm having a little trouble with virtualenv on Mac OS X Yosemite. After I couldn't run virtualenv at all first, I installed Python 3 via brew (previously I installed it via the package on python.org). I linked this installation of python3, updated pip and ran pip3 install virtualenv. When I try to run virtualenv (e.g. $ virtualenv --python=python3 ../virtualenv), I get the following error message.
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 7, in <module>
from virtualenv import main
File "/usr/local/bin/virtualenv.py", line 7, in <module>
from virtualenv import main
ImportError: cannot import name 'main'
Can anybody help me with this?
After my upgrade to Fedora 32 I had the same issue which lead me to this question:
ImportError: cannot import name 'main' from 'virtualenv'
In my case I actually seemed to have both /usr/local/bin/virtualenv as well as $HOME/.local/lib/python3.8/site-packages/virtualenv/__init__.py.
Removing the user virtualenv version and reinstalling it into the system with root fixed the issue:
pip uninstall virtualenv
sudo pip install virtualenv
Your virtualenv executable /usr/local/bin/virtualenv is importing the virtualenv package /usr/local/bin/virtualenv.py. My guess is that package is not the one the executable should really be importing. The reason it is choosing that one is because it is in the same directory.
First, check where the real virtualenv package is. In the python3 terminal:
>>> import virtualenv
>>> virtualenv.__file__
If it is not /usr/local/bin/virtualenv.py, then the simplest way to get /usr/local/bin/virtualenv to import it instead of /usr/local/bin/virtualenv.py is to delete /usr/local/bin/virtualenv.py (or so you can easily undo this if it doesn't work, simply rename virtualenv.py to something else like xvirtualenvx.py).
I received this error after upgrading Ubuntu 18.04 LTS to 20.04 LTS. So there were two problems all at once. First the python version was still running 2.x and doing a simple update or try to uninstall (apt-get remove virtualenv) of virtualenv did not help at all. But I found a solution. First let 20.04 LTS 'know' the times of using old python is over:
sudo apt-get install python-is-python3
Then test it and open a console to get the version string with python -V; by now it should be showing something like Python 3.8.5. Fine.
Next step is to solve the virtualenv problem. I tried to find out, which executable was run with which virtualenv and it showed: $HOME/.local/bin/virtualenv. Hmmkay, somehow the system wasn't using the /usr/bin/virtualenv executable. I thought maybe I let the directory become invisible (a.k.a. renaming) and maybe the system will go on a hunt for an alternative virtualenv running:
mv $HOME/.local/bin/virtualenv /home/USER/.local/bin/virtualenv_OLD
Then I simply changed into a playground-directory and ran virtualenv donaldknuth and behold - it worked. To be sure I ran another which virtualenv and the system returned a /usr/bin/virtualenv. Last check to do was activating the new virtual environment:
source $HOME/playground/donaldknuth/bin/activate
The terminal changed and it worked fine. Solution
EDIT:
Based on Pierre B.'s suggestion you may have to restart your Shell. The command hash -d virtualenv will delete the stored location of virtualenv from the shell's cache and determine the correct path right now. (Sources: https://www.computerhope.com/unix/bash/hash.htm, https://unix.stackexchange.com/questions/5609/how-do-i-clear-bashs-cache-of-paths-to-executables)
Similarly to some others here, I had multiple installations of virtualenv. Not sure where the extra one came from, but I had these two:
/usr/local/bin/virtualenv
/usr/bin/virtualenv
One is from apt install of virtualenv, the other from pip install of virtualenv.
This happened when upgrading to Ubuntu 20.04.
On Linux Mint 20, I had to switch default Python interpreter to python3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
Then remove existing virtualenv and reinstall via pip and python3:
rm ~/.local/bin/virtualenv
apt remove python3-virtualenv
sudo pip install virtualenv

Resources