Virtual env for Django projects (Windows)
In this post, I'm aiming to give you a very clear understanding of a virtual environment and how to use it to set up your Django/Python projects correctly.
-A Brief definition of a virtual environment: skip to practice
I like to see a virtual environment as an empty box that will carry every needed dependency/package in our project. And the reason we're using it is that it's a good practice to separate any needed package from any other already existing package.
So what we're doing is we're creating a place where we are going to store every dependency needed in our project, and we are going to activate that environment each time we want to run our project, so that we actually run it with the installed dependencies in the virtual environment created. Let's dive right into the practice.
All of the code below shall be executed in your CMD.
-Navigate to your project's directory:
-Create the virtual environment:
Python3 -m venv env
This command creates a folder named env/
-Activate your environment:
NB: Seeing a
(env) sign before each line in the CMD is a sign that the virtual environment is active.
-See the python version in the virtual env:
After activating the virtual environment:
pip install django
Note: Your environment should be activated each time you want to download a package so that it is stored inside of the virtual environment.
-To see what packages you've installed:
NB: The virtual env should be active to see what's inside of it.
-Store packages names inside a txt file:
pip freeze => requirements.txt
NB: This step creates a requirements file that contains the needed packages to run your project, it should be helpful when deploying the project.
-Deactivate the virtual environment: