Installing Django

Mac Users Note

Once Python 3 and the virtual environment are installed, the installation steps for Django are identical on both Windows and macOS.

The critical thing to remember with macOS is that system Python is version 2 and Django requires Python 3, so you must be running the Python virtual environment on macOS to run any of the code in this book.

Now we have Python installed and are running a virtual environment, installing Django is super easy, just type the command:

  1. pip install "django>=2.2,<3"

For Django 3, the command is:

  1. pip install "django>=3.0,<4"

If you are not familiar with the pip command, put briefly, it’s the Python package manager and is used to install Python packages. To keep with Python programming tradition, pip is a recursive acronym for “Pip Installs Packages”.

This will instruct pip to install the latest version of Django 2 or Django 3 into your virtual environment. Your command output should look like this (for Django 2.2):

  1. (env_myclub) ...\myclub_project> pip install "django>=2.2,<3.0"
  2. Collecting django<3.0,>=2.2
  3. Downloading .../Django-2.2.12-py3-none-any.whl (7.5MB)
  4. |################################| 7.5MB 2.2MB/s
  5. Collecting pytz (from django<3.0,>=2.2)
  6. Downloading .../pytz-2020.1-py2.py3-none-any.whl (510kB)
  7. |################################| 512kB 3.3MB/s
  8. Collecting sqlparse (from django<3.0,>=2.2)
  9. Downloading .../sqlparse-0.3.1-py2.py3-none-any.whl (40kB)
  10. |################################| 40kB 2.6MB/s
  11. Installing collected packages: pytz, sqlparse, django
  12. Successfully installed django-2.2.12 pytz-2020.1 sqlparse-0.3.1

The Django 3 installation output is identical except for the version numbers.

To test the installation, go to your virtual environment command prompt, start the Python interactive interpreter by typing python and hitting Enter. If the installation was successful, you should be able to import the module django:

  1. (env_myclub) ...\myclub_project>python
  2. Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import django
  5. >>> django.get_version()
  6. '2.2.12' # Your version may be different.
  7. >>> exit()

Don’t forget to exit the Python interpreter when you are done (you can also use CTRL-Z).

You can also check if Django has been installed directly from the command prompt with:

  1. (env_myclub) ...\myclub_project>python -m django --version
  2. 2.2.12 # Your version may be different.