Install Virtualenv
The install of Virtualenv is straight forward. Using pip, you can execute the below command from the terminal.
Alternatively, if using Anaconda you will need to use the below terminal command instead.
Your terminal output should look similar to the below.
Collecting virtualenv
Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 267kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
Bradleys-Mini:~ BradleyPatton$
Create an Environment
Virtualenv has one main command. The below line executed from the terminal will create a new “silo” or virtual Python environment in the Tutorial directory.
You should get some terminal output like below after executing.
Overwriting /Users/BradleyPatton/anaconda/lib/python2.7/site-
packages/Tutorial/lib/python2.7/orig-prefix.txt with new content
New python executable in /Users/BradleyPatton/anaconda/lib/python2.7/site-
packages/Tutorial/bin/python
copying /Users/BradleyPatton/anaconda/bin/python =>
/Users/BradleyPatton/anaconda/lib/python2.7/site-packages/Tutorial/bin/python
copying /Users/BradleyPatton/anaconda/bin/../lib/libpython2.7.dylib =>
/Users/BradleyPatton/anaconda/lib/python2.7/site-packages/Tutorial/lib/
libpython2.7.dylib
Installing setuptools, pip, wheel…done.
The virtualenv command will build a directory structure including a binary, library and include directory for the new virtual environment that was created.
bin lib
include pip-selfcheck.json
Bradleys-Mini:Tutorial BradleyPatton$
/bin
contains your executables most notably Python and pip.
activate easy_install-2.7 python-config
activate.csh pip python2
activate.fish pip2 python2.7
activate_this.py pip2.7 wheel
easy_install python
Bradleys-Mini:bin BradleyPatton$
The /lib
and /include
directories include supporting files for Python and the application to be developed.
Activate Virtualenv
The activation script updates your path so that you can utilize this virtual environment without the hassle of navigating to the directory. It makes it a bit easier to use, but could be skipped if you are a terminal ninja and don’t mind the key strokes.
In the /bin
directory there is an activate BASH script. You can execute using the below.
If you will note from my fumbling below that I had to modify the permission of the file to execute. I used the CHMOD 700 activate
command to update the permissions. You may also need to make this update prior to running the activate script.
activate easy_install-2.7 python-config
activate.csh pip python2
activate.fish pip2 python2.7
activate_this.py pip2.7 wheel
easy_install python
Bradleys-Mini:bin BradleyPatton$ ./activate
-bash: ./activate: Permission denied
Bradleys-Mini:bin BradleyPatton$ sudo ./activate
Password:
sudo: ./activate: command not found
Bradleys-Mini:bin BradleyPatton$ chmod 700 activate
Bradleys-Mini:bin BradleyPatton$ ./activate
Bradleys-Mini:bin BradleyPatton$
Deactivate Virtualenv
To undo the environment variable changes that were made by activate run the following command from the terminal. This will revert your path changes like they never happened. It’s as simple as that.
Removing an Environment
Removing a virtual environment is as simple as rm
. Simply type the following to remove the directory and recursively its contents.
Now What
Well, now you need to install your libraries and application in the new virtual environment. Pip makes sourcing your libraries easy.
I won’t go into the subtleties of pip some of which can be found here, but I will demonstrate a single pip install.
Collecting pandas
Using cached pandas-0.22.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9
_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting numpy>=1.9.0 (from pandas)
Using cached numpy-1.14.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9
_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting python-dateutil (from pandas)
Using cached python_dateutil-2.6.1-py2.py3-none-any.whl
Collecting pytz>=2011k (from pandas)
Using cached pytz-2017.3-py2.py3-none-any.whl
Collecting six>=1.5 (from python-dateutil->pandas)
Using cached six-1.11.0-py2.py3-none-any.whl
Installing collected packages: numpy, six, python-dateutil, pytz, pandas
Successfully installed numpy-1.14.0 pandas-0.22.0
python-dateutil-2.6.1 pytz-2017.3 six-1.11.0
(Tutorial) Bradleys-Mini:bin BradleyPatton$
The following command will open a Python interpreter command line. I will import our new pandas library and check the version. Version 19 is my global pandas version, but as you see from the terminal output, the version used in our Tutorial virtual environement is 22.
Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import pandas
>>> pandas.__version__
u’0.22.0′
>>>
This tutorial should get you started with Virtualenv. I have added the link to the Virtualenv page that can assist with some in depth configuration using parameters and configuration settings that can be used in special circumstances.