It may not be readily obvious, but it’s really important to manage each of your Django projects from within a virtual environment. A virtual environment is a lightweight, safe means of isolating all of the Python code packages associated with your project. Even the versions of Python and Django that you choose for your project are locally installed into a virtual environment; or at least, they should be. Without virtual environments it would be impossible to manage more than one individual development project at a time on your computer because of version conflicts between like packages across different projects.
pip (Python Install Package) is the easiest and most common way to download packages for your project, and it works out of the box with all types of Python virtual environments.
I’ve used Virtualenv as well as venv and find both to be easy to setup and use. We’ll use venv for this article, as follows:
# execute the following from inside a newly-created project folder on your local computer
python3 -m venv venv
Your command prompt will change slightly when the virtual environment is running. You’ll notice the name of your virtual environment, “venv”, placed at the far-left of your command prompt. Your computer will otherwise behave normally. Checking the Python version from within your virtual environment should result in screen output similar to the following:
(venv) mcdaniel@Lawrence-MacBook tmp % python --version
To hopefully shed some light on how the virtual environment works, here’s a screen shot of the folder and files that were added to our project folder after running the command
python3 -m venv venv. Notice how you can trace the path for the command
source venv/bin/activate. Also, take particular note that local copies of (or symlinks to) the principal system executables — pip, and Python — are present in the bin folder.