×

Search anything:

virtualenvwrapper in Python

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

virtualenvwrapper

  • virtualenvwrapper is an addition to the standard virtualenv tool. It streamlines the process of managing your virtual environments by having wrappers for creating and deleting virtual environments and more.
  • Some of the other features are the following.
    • Tab completion for commands.
    • Configurable hooks on all operations.
    • Plugin system.

Table of contents

Why use virtual environments?

  • Virtual environments allow for better management of your work-flows.
  • Decreases the chance to create conflicts of dependencies.
  • Gives structure to your python package bin which is unorganized and potentially near impossible to find anything, or manage.

Why use virtualenvwrapper over virtualenv?

  • virtualenv stores the venv direction inside the project directory where the virtualenvwrapper stores all of the virtual environments together in.

    • This is better as it allows for better management of all of your virtual environments. Find it at the directory shown below.
    $ cd ~/.virtualenvs
    
  • Removes the need to find the activate script to enable the virtual environment every time. Now you only need to type the following command into your terminal.

     $ workon {name_of_enviornment}
    
  • Then to disable the environment it is even easier just simply type the following command into your terminal.

     $ deactivate
    

Getting started

Pip install (pip install virtualenvwrapper)
One thing to note before we start is the supported shells are bash, ksh, and zsh. For windows use virtualenvwrapper-win which is a reimplementation of virtualenvwrapper for windows.

  • To get started first we need to install virtualenvwrapper
     $ sudo pip install virtualenvwrapper
    
  • Now we can start to configure the shell to load the virtualenvwrapper commands in to the shellrc file which may be .bashrc or any equal and just add the following lines to your config file. (Remember to save)
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
    export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
    source /usr/local/bin/virtualenvwrapper.sh
    
  • You can now make your virtual environments with virtualenvwrapper; the installation is complete.
    $ mkvirtualenv {name_of_environment}
    

Demo

  • For this demo we are going to setup a possible virtual environment of a data science engineer.
  • Lets started by creating the environment
    $ mkvirtualenv data_science
    
  • It should automatically put you inside the environment, but in case you exit this is how you would enter it again.
     $ workon data_science
    
  • Now lets install a couple packages to demonstrate a couple of the other features. Notice the indication showing we are in a virtual environment.
     (data_science)$ pip install numpy
     (data_science)$ pip install matplotlib
     ...
     # To view the packages on the environment
     (data_science)$ lssitepackages
     # To delete environment
     (data_science)$ rmvirtualenv data_science
     # Help command which will show all the commands and info on how to use them
     $ virtualenvwrapper
    

Review

Question 1

How do you enable an environment with virtualenvwrapper

Using the activate script
$ env {name_of_environment}
$ workon {name_of_environment}
$ work {name_of_environment}
$ workon {name_of_environment} is the command

Question 2

How do you view the packages in the current virtual environment

(name_of_environment)$ ls sitepackages
(name_of_environment)$ opensitepackages
$ ls sitepackages
(name_of_environment)$ lssitepackages
(name_of_environment)$ lssitepackages is the command.

Advanced

Coverage of some more commands and all of their parameters.

 $ workon [(-c|--cd)|(-n|--no-cd)] [environment_name|"."]

If there are the -c or the --cd flags it is also update the working directory to the project directory. If you use the -n or the --no-cd it will not change your working directory.

If a . is used in place of the environment name then it will use the name of the current working directory.

 $ add2virtualenv directory1 directory2 ...

There are some use cases where you may wish to share your packages with other virtualenv. This allows you to by using the command and listing the directory of the other packages.

 $ setvirtualenvproject [virtualenv_path project_path]

With this article at OpenGenus, you must have the complete idea of virtualenvwrapper in Python.

virtualenvwrapper in Python
Share this