Occasionally, I want to test a new module or I have to use a module that I do not want to install in my Python system, or I have to install a version of a module that I will only be using for a project and then not use again. I could use pip -t to install the modules in a different directory and then manipulate sys.path. Then I have to remember how to uninstall the packages and change my sys.path variable again. This is a little error prone and I find myself always having to check to find out what environment I am in.

There is an easier way. The module virtualenv will create a virtual environment that can be activated and deactivated. When it is activated, modules and packages installed are installed in the environment directory. The python executed starts from the environment directory and the sys.path variable is modified to search in the environment directory first.

virtualenv is easy to install through pip, sudo pip install virtualenv.

Here is a very simple example.

  1. Create the virtual environment.
    • Execute: virtualenv Fibonacci
      • See:

Virtual Python1

This will create a new subdirectory under the current directory, called Fibonacci in the example, with several subdirectories and programs inside to set up the environment.

  1. I tend to cd to the environment directory and make a subdirectory called Project-<project_name> just to keep the virtual environment and the code I want to execute under it in the same place.
    1. Execute: cd Fibonacci
    2. Execute: mkdir Project-Fibonacci
    3. Execute: cd Project-Fibonacci
  1. Activate the environment.
    • Execute: source ../bin/activate
      • See:

Virtual Python2

Notice the (Fibonacci) at the head of the prompt string. This is the notification that you are in a virtual Python environment.

Notice to activate you have to source the activate command. The command is in the bin directory inside the environment directory.

  1. Create the environment.
    • This is the installation of the modules and packages needed for this environment.
    • I suggest you create a pip requirements file as a way to easily keep track of the packages added. Recall that in a requirements file you can specify the version of the package you want.
    • For this example, I am not following good engineering practice.
    • Execute: pip install fibonacci
      • See a lot of stuff.
  1. If you can reach the documentation with pydoc the package is most likely installed correctly.
    • Execute: pydoc fibonacci
      • See:

Virtual Python3

Notice the package is stored under the Fibonacci environment directory.

  1. Work the project.
    • Execute: python
    • Execute: import fibonacci
    • Execute: fibo(10)
      • You should see 55.
    • Execute: <Ctrl-D>

  1. Deactivate virtual environment.
    • Execute: deactivate
    • See:

Virtual Python4

Notice the indicator for being in a Python virtual environment is lost.

  1. Clean up.
    • Leave the environment directory intact until you are done with the project.
    • Make sure you save anything in the Project-<project_name> that you need.
    • Remove the environment directory and all the cleanup is done and you do not have to repair or rollback your Python system.

 

 

Leave a Reply

Your email address will not be published.