This is part two of virtual environments in Python. Part one can be found here.
In the last article, we learned about using virtual environments with the standard library module venv
. and we explored some of the commands you could use to customize your virtual environment. This time we will be using virtualenv
[1], a pip module that creates and manages virtual environments. It has more features than the venv
module, so let's get started.
First, create a new directory for testing purposes, like so:
This is a very simple directory, with a singular python file inside. This file's contents are shown below:
https://gist.github.com/bcb2422fcec4f217f09f612dcf65d9aa
Once you've entered the directory, type the following code in your terminal:
https://gist.github.com/eb327a1377fb8057a3fe7384f3a9401c
Reminder: We use the
pip3
installer to specify which Python installation we'd like to download for.
Now that virtualenv
has been installed, we can use the basic env creating a command to create the environment:
https://gist.github.com/a5d718b07906c6299394c845d0e94806
There's no console output from this command, so you may have to close your directory and open it again to see if it has worked. Also, note that you can call the virtual environment something else than env
; this is just a very common name for virtual environments.
If your directory looks like this, you've succeeded! If you used a different name than env
for your environment, your newly created directory name may vary.
Now that your virtual environment is created, let's activate it with source env/bin/activate
. Remember if your virtual environment's name is not env
, substitute env
with your environment's name.
https://gist.github.com/99b8f7fa5f38f1eccf2ac3e3106c8282
As you can see, there's no console output from this command, but if you pay close attention, you can see that something has changed! There is now (env)
before the prompt, signifying that we're now in the virtual environment. Try to install a package via pip to test it out:
https://gist.github.com/5f0148526840154995bd452b53c308f0
Now we could do all of this with venv
but there are perks to doing this with virtualenv
instead.
venv
is slowervenv
cannot be upgraded via pip, since it is a module of the standard library that ships with Pythonvenv
only works with Python 3.3 or higher; virtualenv will work from Python 2.7 onwards
This is very important; since venv
is a batteries-included module, this means that it cannot be updated, whereas virtualenv
can be updated. It's also important to note that working with distributions of Python before 3.3 is an important feature.
To update virtualenv
, simply type:
https://gist.github.com/db16c9b34bd2c4da14773a4174af6b2c
Success! Now you can use the pip commands covered in article one. Another big advantage to using virtualenv
is that there are many add-ons made for it. One such add-on would be nox
[2], which is a command-line tool for testing within a virtualenv environment.
In this article, we have
References