Both packages can be installed via pip:
pip install virtualenv virtualenvwrapper
To create a virtual environment, you can use the mkvirtualenv command:
This will create a folder called myenv in ~/Envs. This command will automatically put you inside of the environment.
If you want to use a different version of Python, you can specify the version when creating the environment using the -p argument. For me, to create a Python 3.5 environment I use:
mkvirtualenv -p /usr/local/bin/python3.5 py35
This creates an environment named py35 and specifies /usr/local/bin/python3.5 as the Python executable. After we create the environment, run python --version to verify the version of Python being used.
Virtualenvwrapper provides a nice way to create a new project and virtual environment with the same command:
This will create the virtual environment called myproject as well as a project directory. After running this command you'll be working in your virtual environment and cd'd into the project directory.
To enter a virtual environment, you can use the workon command:
To find a list of existing virtual environments, use the lsvirtualenv command:
To get out of the virtual environment, you can use the normal virtualenv command:
Or, you can simply close your shell. When you open a new shell you will land in your original environment. Then, to renter the virtual environment, use the workon command from the previous step.
To remove our virtual environment, use the rmvirtualenv command.
This will remove the virtual environment from ~/Envs.
List virtual environments:
Make a temporary virtual environment:
Copy a virtual environment:
cpvirtualenv [env] [targetenv]
To list all of the site packages from within the virtual environment:
Remove all of the third party packages from the current virtual environment:
For a complete list of commands, refer to the virtualenvwrapper documentation.
If you've been working with Python for a while, you've probably noticed .pyc files in your source code. In fact, you've probably got a line like *.pyc in your .