Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Flask Blueprints

You're reading from   Flask Blueprints Dive into the world of the Flask microframework to develop an array of web applications

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781784394783
Length 198 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Joel Perras Joel Perras
Author Profile Icon Joel Perras
Joel Perras
Arrow right icon
View More author details
Toc

Working with virtualenv

First, we need to make sure that we have the virtualenv tool installed in our local system. This is a simple matter of fetching it from the PyPI repository:

$ pip install virtualenv

Note

For obvious reasons, this package should be installed outside any virtual environments that may already exist.

Creating a new virtual environment

Creating a new virtual environment is straightforward. The following command will create a new folder at the specified path that will contain the necessary structure and scripts, including a full copy of your default Python binary:

$ virtualenv <path/to/env/directory>

If we want to create an environment that lives at ~/envs/testing, we will first ensure that the parent directory exists and then invoke the following command:

$ mkdir -p ~/envs
$ virtualenv ~/envs/testing

In Python 3.3+, a mostly API-compatible version of the virtualenv tool was added to the default language packages. The name of the module is venv, however, the name of the script that allows you to create a virtual environment is pyvenv and can be invoked in a similar way as the previously discussed virtualenv tool, as follows:

$ mkdir -p ~/envs
$ pyvenv ~/envs/testing

Activating and deactivating virtual environments

Creating a virtual environment does not automatically activate it. Once the environment is created, we need to activate it so that any modifications to the Python environment (for example, installing packages) will occur in the isolated environment instead of our system global one. By default, the activation of a virtual environment will alter the prompt string ($PS1) of the currently active user so that it displays the name of the sourced virtual environment:

$ source ~/envs/testing/bin/activate
(testing) $ # Command prompt modified to display current virtualenv

The command is the same for Python 3.3+:

$ source ~/envs/testing/bin/activate
(testing) $ # Command prompt modified to display current virtualenv

When you run the above command, the following series of steps occurs:

  1. Deactivates any already activated environment.
  2. Prepends your $PATH variable with the location of the virtualenv bin/ directory, for example, ~/envs/testing/bin:$PATH.
  3. Unsets $PYTHONHOME if it exists.
  4. Modifies your interactive shell prompt so that it includes the name of the currently active virtualenv.

As a result of the $PATH environment variable manipulations, the Python and pip binaries (and whatever other binaries that were installed via pip), which have been invoked via the shell where the environment was activated, will be the ones contained in ~/envs/testing/bin.

Adding packages to an existing environment

We can easily add packages to a virtual environment by simply activating it and then invoking pip in the following way:

$ source ~/envs/testing/bin/activate
(testing)$ pip install numpy

This will install the numpy package to the testing environment, and only the testing environment. Your global system packages will be unaffected, as well as any other existing environments.

Uninstalling packages from an existing environment

Uninstalling a pip package is straightforward as well:

$ source ~/envs/testing/bin/activate
(testing)$ pip uninstall numpy

This will remove the numpy package from the testing environment only.

Here is one relatively major place where the Python package management falls short: uninstalling a package does not uninstall its dependencies. For example, if you install package A and it installs dependent packages B and C, uninstalling package A at a later time will not uninstall B and C.

You have been reading a chapter from
Flask Blueprints
Published in: Nov 2015
Publisher:
ISBN-13: 9781784394783
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image