Python Virtual Environment

October 25, 2018

When starting a new project I have found it useful to create a virtual environment for Python so that only the packages needed for the project are used. This means that when it is 'production ready' there are unnecessary requirements.

The package to do this is virtualenv.

This was done on Windows 10 running Ubuntu as the WSL (Windows Subsystem Linux).


User Guide

Installing virtualenv

sudo pip3 install virtualenv

The directory '/home/hyperopotic/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

The directory '/home/hyperopotic/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl (1.9MB)
    100% |████████████████████████████████| 1.9MB 749kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.0.0

Creating a virtual environment

I like to create a directory where all my virtual environments live.
mkdir ~/Environments

Then create the virtual environment
virtualenv <name of your environment - it can be anything>

For example

user@system:/Environments$ virtualenv test_env
Using base prefix '/usr'
New python executable in 
/Environments/test_env/bin/python3
Also creating executable in 
/Environments/test_env/bin/python
Installing setuptools, pip, wheel...done.

Now the environment has been created and pip, setuptools and wheel have been installed in to the environment.

Accessing the environment

Entering the environment is as follows
source <name of your environmnet>/bin/activate
Example

user@system:/Environments$ source test_env/bin/activate
(test_env) user@system:/Environments$

You can tell you are in the new environment because it says (test_env) but you can check pip using pip list and which python3.

(test_env) user@system:/Environments$ pip list
Package    Version
---------- -------
pip        18.1
setuptools 40.4.3
wheel      0.32.1

(test_env) user@system:/Environments$ which python
/mnt/c/Users/adam.jarvis/Documents/Environments/test_env/bin/python

(test_env) user@system:/Environments$

Now you can install packages and use python and pip packages as you normally would!

Leaving the virtual environment

This is easy... Real easy.
deactivate
When in the environment just run that command.

(test_env) user@system:/Environments$ deactivate
user@system:/Environments$

Deleting a virtual environment

Just remove the environment for the folder as you would normally do.

user@system:/Environments$ ls
test_env

user@system:/Environments$ rm -r test_env/

user@system:/Environments$ ls
user@system:/Environments$
Share on Google+