How to Django —Virtual Environments
Creating Virtual Environments in Python for Django
During your time learning Python, you will without a doubt come across the need to use virtual environments.
A virtual environment is effectively a folder containing all the necessary packages a python project would need.
As you’re building and running several different Python applications, you will notice that not every application has been built using the same version of a package. This is what makes virtual environments extremely effective.
Rather than uninstalling and installing a specific package version (which would make managing multiple applications difficult) — you, as the user can create a separate environment for Python so that when you run one program, you can just switch to the environment which has the correct packages. You can potentially even run multiple instances of python on different virtual environments.
So now begs the question…
How do I make one?
From my experience, virtual environments are best stored one of two places:
- Within the project’s folder
- In a main virtual environment folder — labelled by project name
To make one, you must have python already installed, to which you may do the following:
- Open the Command Prompt
- pip install the virtual environment package
In my case, I created a virtual environment folder for all environments I will ever make — called “VE”. Inside it, I created a project folder — “coxlabs” and another folder for “Scripts”. It’s completely up to you on how you structure your folders.
VE > coxlabs > Scripts
Now, let’s install virtualenv so that we can use it:
pip install virtualenv
- Navigate to either your project’s folder or to the main virtual environment folder in the subfolder with the project’s name
- Create a virtual environment (no spaces allowed)
virtualenv { insert name here }//Example:virtualenv myvirtualenv
There’s a chance that the command may give you an error message:
'virtualenv' is not recognized as an internal or external command
A way around this is to instead write it from Python which then accesses the module:
python -m virtualenv { insert name here }//Example:python -m virtualenv myvirtualenv
- Navigate into the virtual environment folder you just created
cd { insert name here }
cd Scripts//Example:cd myvirtualenv
cd scripts
- activate the virtual environment
activate
- You will notice that the start of the last line now follows the name of your virtual environment folder.
- Now you may install whichever Python packages you wish for that project using
pip install
For example, we can install Django version 3.0.3
pip install Django==3.0.3
Now if we wanted to build a django project using this version, we can jump into this virtual environment to create one.
We can even create another virtual environment which uses Django 2.2.
- To exit, simply type
deactivate
in the command line which will return you to the directory. Or you may exit the program by typingexit
From now on, based on the project you’re working on, you can simply navigate to the virtual environment folder you created for that project. From here you can activate and just keep working without having to worry about whether the package is the correct version!
Summary
Every time you create a new Django website, I recommend that you use the same virtual environment unless if they use different versions of the same libraries.
Virtual environments save you from having to uninstall and reinstall different versions of a package on your computer which can be beneficial when running other people’s programs — which use specific versions of packages.