Introduction
Although I use Python an awful lot, I have not used Python virtual environments for years. This is because I tend to do most of my development work in Docker containers. However, I recently started a project for which I couldn't use Docker, and so I needed to install and set up virtual environments on my PC.
I installed virtualenv and virtualenvwrapper one evening. Everything seemed fine and I shut the computer down for the night. But the next day, when I started everything up again, there were surprisingly problems with the virtual environment which needed fixing.
And so what follows are my notes from this experience, in the hope that they may help if anyone runs in to the same or similar issues.
Installation
I broadly followed the steps in this page in the Python documentation to install virtualenv and virtualenvwrapper.
Intalling virtualenv seemed to go well, with no problems or apparent errors.
Although it said that virtualenvwrapper was successfully installed, during the installation it came up with "error: invalid command 'bdist_wheel'" and "Failed building wheel for virtualenvwrapper". This was overcome by the following...
pip install wheel
pip install virtualenvwrapper
virtualenvwrapper was now installed with no reported errors.
I set the WORKON_HOME environment variable as follows...
export WORKON_HOME=/mnt/f7e42252-3626-4c40-bbe2-d8156ea14562/learning/python/envs
But the following source command (as copied from the docs) failed with "no such file or directory".
source /usr/local/bin/virtualenvwrapper.sh
It seemed that the virtualenvwrapper.sh file wasn't in the standard location. I located it with the command...
whereis virtualenvwrapper
... and changed to source command to...
source /home/phil/.local/bin/virtualenvwrapper.sh
I ran the following so that no globally installed packages would be available in the created environments...
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
Problems Discovered the Next Day
Problems arose when I started to use this the next day.
Firstly, I got the following errors...
mkvirtualenv: command not found
workon: command not found
The accepted answer on this stackoverflow page (and the first reply to that answer) provided the solution to this. The following code was placed in my .bashrc file...
# load virtualenvwrapper for python (after custom PATHs)
venvwrap="virtualenvwrapper.sh"
/usr/bin/which -a $venvwrap
if [ $? -eq 0 ]; then
venvwrap=`/usr/bin/which $venvwrap`
source $venvwrap
fi
After running...
source ~/.bashrc
... mkvirtualenv and workon were no longer not found. However, just running the command workon gave no output. It should have given a list of the virtual environments that I had previously created.
I realised that my $WORKON_HOME environment variable no longer pointed to the correct place. I added the command...
export WORKON_HOME=/mnt/f7e42252-3626-4c40-bbe2-d8156ea14562/learning/python/envs
... to my .bashrc file, which is, of course, what I should have done the provious evening, instead of just running it the once on the command line as I had. Checking the $VIRTUALENVWRAPPER_VIRTUALENV_ARGS, this was no longer correct as well, so I also added the following to my .bashrc file...
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
Both of these lines were added above the "# load virtualenvwrapper" block.
After closing all of my bash terminals, I opened a new one and confirmed that the two environment variables were now correctly set. I checked back that mkvirtualenv and workon were still working, and indeed they were.
Creating Virtual Environments
mkvirtualenv temp
... created a virtual environment with Python 2.7.15.
However, when I attempted to created a Python 3 virtual environement with ...
mkvirtualenv temp3 --python=/usr/bin/python3
... I got the following error: "ModuleNotFoundError: No module named 'distutils.spawn'". This was fixed by installing python3-distutils...
sudo apt-get install python3-distutils
... at which point ...
mkvirtualenv temp3 --python=/usr/bin/python3
worked as expected and gave my a Python 3 environment.