Installing virtualenv and custom modules in Python

Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that doesn't share libraries with other virtualenv environments (and optionally doesn't access the globally installed libraries either).

Virtualenv is the easiest and recommended way to configure a custom Python environment.

Installing Virtualenv

You can use pip to install virtualenv. Run the following command in your user's directory:

[server]$ pip install virtualenv

Once you’ve installed virtualenv, you can cd into your website directory where you’ll set up a new virtualenv:

[server]$ virtualenv $HOME/<domain>/env

This creates a local copy of your environment specific to this website. While working on this website, you should activate the local environment in order to make sure you're working with the right versions of your tools and packages. Run the following to activate your local environment:

[server]$ source $HOME/example.com/env/bin/activate

The name of the current virtual environment will now appear on the left of the prompt. For example, (env)servername:your_project UserName$) lets you know that it’s active. From now on, any package that you install using pip is placed in the env folder, isolated from the global Python installation.

You can then install packages as usual.

If you are done working in the virtual environment for the moment, you can deactivate it:

[server]$ deactivate
  • This puts you back to the system’s default Python interpreter with all its installed libraries.
  • To delete a virtual environment, just delete its folder (in this case, it would be rm -rf env).

Installing custom modules

It’s recommended that you use pip to easily install custom modules. For example:

[server]$ pip install <module>

For example, (e.g., MySQL-python if you're going to use a MySQL database) for your website using pip:

[server]$ pip install MySQL-python

View the following link for further details:

See also