Setting up a virtual environment for your Django Project
One of the tasks that could be confusing for a beginner starting out with Python on the Windows Operating System (OS) is creating a virtual environment for projects.
In this article, we will go through how to create a virtual environment for a Django project in Windows 10
virtualenv
: is a Python tool used for creating isolated environments. This separates each project from another so that the tools and actions active in one project environment do not have an effect in another environment except they are installed or activated in the other environment.
Prerequisites:
- Have a computer that has Windows OS
- Have Python distribution installed on your computer already
Table of Content
- Install Virtualenv
- Locate where Python is installed on your PC
- Create a virtual Python Environment for your Project
- Activating the Virtual Environment
- Create a Django Project
Install Virtualenv
To install virtualenv
, use the pip
package manager that comes pre-installed with Python. Use the following command on the command prompt. You can access the command prompt by simply search for cmd
on your Start menu. Then, click to open.
pip install virtualenv
After successful installation, virtualenv
should now be in the Scripts
folder inside the Python installation folder on your local disk.
Great! Now, let’s move to the new line of action.
Locate where Python is installed on your PC
While installing a Python software distribution on your PC, if you selected the option to ADD PYTHON TO PATH
, then you would find its location in the PATH
variable.
Otherwise, you will have to look into your local disk to check for its path. The location would be in a path that looks like the following:
C:\Users\%username%\AppData\Local\Programs\Python\Python37\python.exe
Therefore, you need to find python.exe
in the location that looks like the above and take a note of it. Now, we shall create a virtual environment for our project
Create a virtual Python Environment for your Project
To create a virtual environment for you, change directory to the place where you want your Django project to be. Use the cd
command on your command prompt to move to the said directory as follows:
cd first_project
Now, you should use the virtualenv
command appended with --python
to create a new Python environment for your project. You also need to have the Python installation path that you noted from Step 2 above in the same line of command. Then finish off with a name for your new virtual environment, we can call it env
for example. See the following:
virtualenv --python C:\Path\To\Python\python.exe env
Activate the Virtual Environment
At this point, you need to activate the virtual environment for you to be able to work in it. Activate it by:
.\venv\Scripts\activate
So, your command line will change to show the activate environment name like the following:
(env) C:\Users\%username%\Path\To\Project\Directory>
Nice! Now, you can create your Django project and do any other thing that you want active inside the virtual environment.
However, you can deactivate when you are done working or want to leave the virtual environment. Do it this way:
deactivate
Now, let’s go forward into creating a Django project in the new virtual environment.
Create a Django Project
Firstly, install Django here. The installed Django package will only be active in this virtual environment and no other place on your PC.
pip install Django
You can check installed packages by creating a requirements.txt
file. This file contains a list of all the installed packages and their respective versions.
pip freeze > requirements.txt
Now, you can create a Django project. Let’s call our project myproject
for example:
django-admin startproject myproject
That’s it. You have a Django project in a virtual environment already. Congratulations!