As a Django developer, your development environment is your foundation, get it right, and everything flows smoothly; cut corners, and you'll face endless frustration.

In this article I will take you through setting up a professional Django development environment that will work for projects of any size. You will learn how to create and use virtual environments, install Django, use the most suited IDE/Editor for Django, e.tc 

Each Django release specifies which Python versions it supports, you should normally check for compatibility issues on the official docs before deciding what version of either to go with.  In absence of compatibility issues, it is recommended that you use the latest versions of both Python and Django, this is because they may have bug fixes and performance benefits that older versions may lack. 

To see the version of Python you are running, execute the following command from the command-line\shell.

python --version

Virtual Environments

Imagine working on two projects simultaneously, each requiring different Django versions and  packages. Without virtual environments, this common scenario becomes a dependency nightmare. Virtual environments solve this by creating isolated Python installations for each project.

The built-in venv module is the most direct tool for creating virtual environments in Python, and we will use it in this article. venv is a builtin module, meaning that you don't need to perform any kind of installation to use it.

To create a virtual environment you simply need to run the following command from the commandline in your project's directory.

python -m venv <name>

Where <name> is your choice name for the environment. The convention is to call the environment venv or env. So normally, the command would look as follows.

python -m venv venv 
  
     or

python -m venv env

In a practical real world  scenario you would need to run the following commands(it is recommended  that you be on the Desktop directory).

mkdir demo
cd demo
python -m venv venv

You can type and run the above commands in succession or simply put all of them in a .bat file(if you are on a Windows platform) and run them at once.

The above commands does the following:

  • mkdir demo - Creates a directory called demo in the current directory(where the commandline is).
  • cd demo - Moves the commandline to the demo directory
  • python -m venv venv - Creates a virtual environment called venv in the demo directory.

Example of running the above commands in a commandline session:

Commandline session for creating virtual environment

If you open the demo directory using the file explorer, you will see that a new directory(venv) has been created there, this is the virtual environment. 

Activating a virtual environment

To activate the created virtual environment, you should type/paste the following command from the demo directory where we created the environment.

venv\scripts\activate

If the activation is successful, you will now see that each of the cmd's lines are prefixed with the name of the environment in brackets i.e (venv)

In case that command doesn't work and you are on a windows platform, You can instead run it as shown below:

venv\scripts\activate.bat

Once the environment is created, you can now safely install packages in it without affecting the main environment. Note, any package installed inside the environment will not be accessible outside that environment, you must activate the environment first in order to access its packages.

Installing Django in a virtual environment

We have already looked at how to create and activate virtual environments, let's now install Django in the environment. This is simple, you just need to have the environment activated, then run the following pip command to install Django.

pip install django

The above command will install the latest version of Django, you can instead explicitly specify a particular version as follows:

pip install django==5.0.4

 The above command will install Django version 5.0.4

The requirements.txt file

While running the above command(s) directly on the commandline works, in a professional set-up it is more convenient to  place the Django version and all other dependencies  in a requirements.txt file. This file becomes the single source of truth for your project's Python dependencies.

Create the requirements.txt file in the demo directory and add the following lines.

requirements.txt
#requirements.txt

django==5.0.4
requests==2.31.0

In the above case, we have added two packages  django and requests with the required versions inside requirements.txt file. Not e the syntax of the lines, that is how it should be i.e  package==version, If you want the latest version to be installed you should just enter the name of the package without version.

requirements.txt
#requirements.txt

django
requests==2.31.0

In the above case, the latest version of Django while an specific version is explicitly specified for requests.

To actually install the packages specified in requirements.txt file. You should run the following command.

pip install -r requirements.txt

If you run the command with the previous requirements.txt, Both Django and requests will be installed.

You can run the following command to verify the installed version of Django.

djang-admin --version

Choosing an IDE / Editor

Once you have a virtual environment ready and Django installed, you are now ready to start writing code. There are a lot of IDE and text-editors available, your choice can significantly affect your productivity.

In this article, we will not cover all the IDEs and Text-Editors out there. We will talk about the two most commonly used i.e VS Code(IDE) and SublimeText(text-editor) 

VS Code

VS Code(developed and maintained by Microsoft) has emerged as the most popular choice among Django developers. This is due to its perfect balance of lightweight performance and rich features.

You can download the VSCode installer from the official website here, then proceed with the installation process.

After installing the IDE, you should install the following must-have extensions for Django developers.

  • Python (Microsoft's official extension)

  • Django Template Support

  • GitLens (for version control visibility)

SublimeText

Sublime Text is a light-weight text editor, for minimalist developers who value speed and simplicity.

You can download and install the editor from the official website.

After you have installed Sublime Text, proceed to install these essential packages from the package-controll:

  • Django Syntax Highlighting: The "Django" package provides proper template syntax coloring

  • SublimeLinter: Base linter framework

  • SublimeLinter-flake8: Python style checking

  • GitGutter: Version control indicators

  • Djaneiro: Django-specific snippets.

In the next article, you will write your very first Django app.