Installing Django without any Errors

 Before you can start learning Django, you need to install some software on your system:

    1. Install Python 

    2. Install a Python Virtual Environment; and

    3. Install Django

I've written this chapter mostly for those running Windows, as most users are on Windows. If you're on Linux or macOS you can start from Django's own installation process


Installing Python

A lot of Windows application use Python, so it may already be installed on your system. you can check this out by opening command prompt (or PowerShell) and typing python at the prompt.

If python isn't installed you will get a message saying that Windows can't find Python. If python is installed, the python command will open the Python interactive interpreter:


Assuming Python 3 is not installed on your system, you first need to get the installer from here and click the big yellow button that says "Download Python 3.x.x".

At the time of writing latest version of Python is 3.10.1, but it may have been updated by the time you read this. Now open the file you just downloaded to start the installation process.

Make sure "Add Python 3.10.1 to PATH" is checked before installing.


This will solve most problems that arise from the incorrect mapping of pythonpath (an important variable for Python Installations) for Windows.

Once installed restart your system, open a command prompt and run the command python to verify and close the Command Prompt.



Creating a Python Virtual Environment

When you are writing new software programs, its possible to modify dependencies and environment variables that your software depends on. This can cause many problems, so should be avoided. A Python Virtual Environment  solves this problem by wrapping all the dependencies and environment variables that your new software needs into a filesystem separate from rest of the software on your computer.

The virtual environment tool in Python is called venv, but before we setup venv, we need to create our project folder.


Creating a Project Folder

Our project folder will contain not only our virtual environment, but all the code and media for our Django Application.

The project folder can go anywhere on your computer, although it is highly recommended to create it somewhere in your user directory (for ease, I would personally recommend the Desktop). Give it a name that makes sense to you. Here I just named it "Your Project"

- Open the folder

- Copy the folder path

- use the command cd followed by the path you just copied (this will change your current working directory)



- Now use the following command to create the virtual environment with name "myvenv" (again you can choose any name that makes sense to you)

python -m venv myvenv

the -m option tells python to run the venv module as script. Once done you will notice a folder named "myvenv" created in your current working directory which contains the following files and folders:


Now to activate your venv, you need to run the activate script inside the Scripts folder.

To do this simply change your current working directory to Scripts folder and type:

activate

Your command prompt will now look like this:


And you have successfully created and activated your Virtual Environment.

If you want to exit the venv, just type deactivate at the command prompt.


Installing Django

Now we have Python installed and are running a virtual environment, installing Django is super easy. Change your current working directory to the project folder you created and just type the command:

pip install django

Or if you want a particular version, type:

pip install django== (your desired version number ex 2.7.1)

 You will see a similar output except for the version numbers:


To test the installation, just type:

django-admin --version

 And you should get a similar output:



Post a Comment (0)
Previous Post Next Post