Select Your OS:
VirtualEnv allows you us to create isolated Python environments, where a project's dependencies, executables, and even python version are self-contained. This keeps the global filesystem safe and allows us to create multiple Python projects without worrying about potential conflicts.
Before we can use Virtualenv, we need to install Python, so make sure not to skip the prerequisites.
- A 32-bit or 64-bit computer with at least 8GB of RAM. 16GB or more is recommended.
- At least 16GB of free disk space.
We're now ready to install Virtualenv.
# first check if the universe repository is enabled sudo nano /etc/apt/sources.list # if not, modify the file so it does deb http://us.archive.ubuntu.com/ubuntu xenial main universe # update our system and install sudo apt-get update sudo apt-get install virtualenv
sudo pip install virtualenv
Finally, we can start up a Virtualenv. For training purposes, our project's name will be "project". Once you get the hang of it, you may name your project anything you like.
cd ~ mkdir project cd project virtualenv --python=/usr/bin/python2 venv source venv/bin/activate
cd ~ mkdir project cd project virtualenv --python=/usr/local/lib/python2.7 venv source venv/bin/activate
Notice that your username in the command line is now prepended with the Virtualenv session you've activated for that terminal session. Run
ls to confirm that the
venv folder was created.
venv folder contains all the files and dependencies for our project, as well as the Virtualenv files. Every project we contain will have its own
venv folder, with its own unique dependencies.
Use the following commands to exit.
# from any directory deactivate
Use the following commands to re-enter the Virtualenv.
# from the same directory as the venv folder source venv/bin/activate
- Fluent Python: Clear, Concise, and Effective Programming
- Python Cookbook: Recipes for Mastering Python 3
- Introducing Python: Modern Computing in Simple Packages
- Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)
- Head First Python: A Brain-Friendly Guide