Continuing on in my series of developing A Django Project on using Windows 10 as your operating system– we are going to move forward by creating our virtual environment and installing Django on it.
If you are not familiar with this concept you may find this article helpful for an in-depth explanation on virtual environments, but for now I will try to give a brief explanation: Django has several dependencies it requires in order to run and one Django project’s dependencies will likely be different from another’s. A virtual environment allows you to create a compartmentalized environment for a specific project’s dependencies to live and be maintained. This allows you to run/develop more than one Django project at a time even if they have different dependencies and software versions.
For example, if you were building a new Django project you would create a virtual environment and install the latest version of Django. If at some point you were tasked with modifying an existing Django project you may find that it is running a different software version and different packages altogether. In this case you would deactivate the virtual environment for your brand new Django project and activate one with the correct software versions/necessary packages for the existing Django project you are going to work on without having to drastically change your development environment.
Creating the Virtual Environment
- Open up Administrator Command Prompt: Click on the search box near the Windows 10 start button or click on the start button and begin typing “cmd” then right-click and choose “Run as Administrator.”
- Type ‘mkvirtualenv projectname’.
This will create and activate your virtual environment with the name “projectname.” You can run this command from any directory as it will create it in a default location. This is determined by the virtualenvwrapper-win package we installed in the previous article. To learn more about virtualenvwrapper-win and change the directory virtual environments are created you can checkout the Github repository for the project. While active, any packages you install will be ‘contained’ to this environment.
- In the same Administrator Command Prompt change to the directory you wish to store your Django projects by typing “cd directoryname” or “cd c:\directoryname” depending on your set-up.
This should not be the same directory as your virtual environment (a good example would be ‘C:\Development\Projects’).
If you are trying to change directory to another drive in the Windows Command Prompt you would first type: “d:”, and then you would type “cd d:\your-directory\name”
- Create the directory for the new Django project: “mkdir projectname”.
- Change/move to our project’s new directory: “cd projectname”.
- Tell our virtual environment what directory our project lives in: “setprojectdir .” Note: the command includes the period after setprojectdir: “setprojectdir .”
This command associates whatever project lives in this directory with our currently active virtual environment. Another nice thing about this is when you open up your Administrator Windows Command Prompt and activate your virtual environment you will automatically be moved to the project directory.
Installing Django and Starting a Project
- From the Administrator Windows Command Prompt, while in your project directory with your virtual environment still active– type: “pip install Django”
- While still in your project directory type: “django-admin startproject projectname”
This will create a new directory within your project directory (it’s alright if it’s the same) and set-up a base Django project.
- Change to your new Django project’s install: “cd projectname”.
This is the directory that contains the file “manage.py”.
- Let’s start your Django project’s server: “python manage.py runserver” (some users may need to type “python3 manage.py runserver”).
- If all goes well you will see a few messages scroll including “Starting development server at http://127.0.0.1:8000/”– enter than address into your browser to see your new Django install.
- After confirming the Django install works you can simply return to the Command Prompt and press “CTRL+C” to cancel/kill the server.
Working With Virtual Environments
- If you have been following each step your virtual environment should still be active. Let’s go ahead and deactivate it if it isn’t already by typing into our Administrator Windows Command Prompt: “deactivate”.
If you closed your Windows Command Prompt it will also deactivate the virtual environment.
- In any case, if you now wish to work on your Django project that you previously set-up you would simply open an Administrator Windows Command Prompt and type: “workon projectname”.
This will activate the related virtual environment and take you to that project’s directory.
- If you would like to start your Django server again you will need to change directory into the Django project and type the command: “python manage.py runserver” (or for some users “python3 manage.py runserver”).
- When you are done working you can press “Ctrl+C” to kill the server and then type “deactivate” to deactivate your virtual environment or simply close the command prompt window.
Congratulations! You have your Windows 10 Python/Django development environment configured, you have the ability to install and manage virtual environments to keep your software versions and packages separate and specific to each Django project, and you have a working Django base project up and running.