Isolate your program from whatever packages people have installed on their OS.


Virtualenv is a python development tool hosted at PyPI. Virtualenv generates a script that allows you to enter and exit a virtual python environment. By this I mean that Virtualenv’s scripts manipulate your path so that you can easily develop in an isolated python environment where you have the ability to install your own python interpreter, and - using pip - install packages from PyPI. Then when you are finished developing you can disable the virtual environment from your interactive terminal then - with a small modification to how you run your programs - deploy an isolated installation that should be very portable.

Installing

As root run the following command:

pip install virtualenv

Creating a Virtual Environment

The basic command to create a new Virtual Environment is:

virtualenv [options] DEST_DIR

Some options you may be interested in are:

-p PYTHON_EXE :: Interpreter path to use.
--system-site-packages :: Use all system installed packages.  (be careful)
--prompt PROMPT :: Alternate prompt.

Also the --relocatable may be useful t omake an existing virtual environment use all relative paths.

Activating

When you activate a virtual environment the activation script modifies your path so that the settings of the virtual environment are the first to be used when executing python programs from that shell. To activate the environment you need to source the <virtual_env_dir>/bin/activate script.

After sourcing the activation script your shell will use the configured virtual environment paths before your system paths.

Developing

Once you’ve created and activated your virtual environment you can use pip to install some packages, tools and various other things to your environment. You can develop scripts just like you normally would using the modified shell environment.

Deactivating

Once you’re happy with your scripts, or when you need to take a break you can deactivate the environment by running the bash function deactivate. This function is defined by the activation script that you ran in the previous steps.

Deploying

To deploy your project make sure everything is using the right relative paths and you’ve included all the necessary dependencies, including the virtual environment directories. Finally all you need to do is execute your scripts with the PYTHONPATH and python executable set to the virtual environment.

This way when your project is run, it will use the virtual environment but you don’t have to modify much, if any, of your main environment.


" "