Django installation

Note If you're using a Chromebook, skip this chapter and make sure you follow the Chromebook Setup instructions.

Note If you already worked through the installation steps then you've already done this – you can go straight to the next chapter!

Part of this section is based on tutorials by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots).

Part of this section is based on the django-marcador tutorial licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. The django-marcador tutorial is copyrighted by Markus Zapke-GrΓΌndemann et al.

Virtual environment

Before we install Django we will get you to install an extremely useful tool to help keep your coding environment tidy on your computer. It's possible to skip this step, but it's highly recommended. Starting with the best possible setup will save you a lot of trouble in the future!

So, let's create a virtual environment (also called a virtualenv). Virtualenv will isolate your Python/Django setup on a per-project basis. This means that any changes you make to one website won't affect any others you're also developing. Neat, right?

All you need to do is find a directory in which you want to create the virtualenv; your home directory, for example. On Windows, it might look like C:\Users\Name\ (where Name is the name of your login).

NOTE: On Windows, make sure that this directory does not contain accented or special characters; if your username contains accented characters, use a different directory, for example, C:\djangogirls.

For this tutorial we will be using a new directory djangogirls from your home directory:

command-line

$ mkdir djangogirls
$ cd djangogirls

We will make a virtualenv called myvenv. The general command will be in the format:

command-line

$ python3 -m venv myvenv
Virtual environment: Windows

To create a new virtualenv, you need to open the command prompt and run python -m venv myvenv. It will look like this:

command-line

C:\Users\Name\djangogirls> python -m venv myvenv

Where myvenv is the name of your virtualenv. You can use any other name, but stick to lowercase and use no spaces, accents or special characters. It is also a good idea to keep the name short – you'll be referencing it a lot!

Virtual environment: Linux and OS X

We can create a virtualenv on both Linux and OS X by running python3 -m venv myvenv. It will look like this:

command-line

$ python3 -m venv myvenv

myvenv is the name of your virtualenv. You can use any other name, but stick to lowercase and use no spaces. It is also a good idea to keep the name short as you'll be referencing it a lot!

NOTE: On some versions of Debian/Ubuntu you may receive the following error:

command-line

The virtual environment was not created successfully because ensurepip is not available.  On Debian/Ubuntu systems, you need to install the python3-venv package using the following command.
   apt install python3-venv
You may need to use sudo with that command.  After installing the python3-venv package, recreate your virtual environment.

In this case, follow the instructions above and install the python3-venv package:

command-line

$ sudo apt install python3-venv

NOTE: On some versions of Debian/Ubuntu initiating the virtual environment like this currently gives the following error:

command-line

Error: Command '['/home/eddie/Slask/tmp/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1

To get around this, use the virtualenv command instead.

command-line

$ sudo apt install python-virtualenv
$ virtualenv --python=python3.12 myvenv

NOTE: If you get an error like

command-line

E: Unable to locate package python3-venv

then instead run:

command-line

sudo apt install python3.12-venv

Working with virtualenv

The command above will create a directory called myvenv (or whatever name you chose) that contains our virtual environment (basically a bunch of directories and files).

Working with virtualenv: Windows

Start your virtual environment by running:

command-line

C:\Users\Name\djangogirls> myvenv\Scripts\activate

NOTE: On Windows 10 you might get an error in the Windows PowerShell that says execution of scripts is disabled on this system. In this case, open another Windows PowerShell with the "Run as Administrator" option. Then try typing the following command before starting your virtual environment:

command-line

C:\WINDOWS\system32> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    Execution Policy Change
    The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy? [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A

NOTE: For users of the popular editor VS Code, which comes with an integrated terminal based off windows PowerShell, if you wish to stick with the integrated terminal, you may run the following command to activate your virtual environment:

$ . myvenv\Scripts\activate.ps1

The advantage is that you don't have to switch between editor windows and command-line windows

Working with virtualenv: Linux and OS X

Start your virtual environment by running:

command-line

$ source myvenv/bin/activate

Remember to replace myvenv with your chosen virtualenv name!

NOTE: If the command source is not available, try doing this instead:

command-line

$ . myvenv/bin/activate

You will know that you have virtualenv started when you see that the prompt in your console is prefixed with (myvenv).

When working within a virtual environment, python will automatically refer to the correct version so you can use python instead of python3.

OK, we have all important dependencies in place. We can finally install Django!

Installing Django

Now that you have your virtualenv started, you can install Django.

Before we do that, we should make sure we have the latest version of pip, the software that we use to install Django:

command-line

(myvenv) ~$ python -m pip install --upgrade pip

Installing packages with requirements

A requirements file keeps a list of dependencies to be installed using pip install:

First create a requirements.txt file inside of the djangogirls/ folder, using the code editor that you installed earlier. You do this by opening a new file in the code editor and then saving it as requirements.txt in the djangogirls/ folder. Your directory will look like this:

djangogirls
β”œβ”€β”€ myvenv
β”‚   └── ...
└───requirements.txt

In your djangogirls/requirements.txt file you should add the following text:

djangogirls/requirements.txt

Django~=4.2.11

Now, run pip install -r requirements.txt to install Django.

command-line

(myvenv) ~$ pip install -r requirements.txt
Collecting Django~=4.2.11 (from -r requirements.txt (line 1))
  Downloading Django-4.2.11-py3-none-any.whl (7.9MB)
Installing collected packages: Django
Successfully installed Django-4.2.11
Installing Django: Windows

If you get an error when calling pip on Windows, please check if your project pathname contains spaces, accents or special characters (for example, C:\Users\User Name\djangogirls). If it does, please consider using another place without spaces, accents or special characters (suggestion: C:\djangogirls). Create a new virtualenv in the new directory, then delete the old one and try the above command again. (Moving the virtualenv directory won't work since virtualenv uses absolute paths.)

Installing Django: Windows 8 and Windows 10

Your command line might freeze when you try to install Django. If this happens, instead of the above command use:

command-line

C:\Users\Name\djangogirls> python -m pip install -r requirements.txt
Installing Django: Linux

If you get an error when calling pip on Ubuntu 12.04 please run python -m pip install -U --force-reinstall pip to fix the pip installation in the virtualenv.

That's it! You're now (finally) ready to create a Django application!

results matching ""

    No results matching ""