Beginning Django Tutorial – Lesson 3 - Python Django Tutorials

Beginning Django Tutorial – Lesson 3

Installing Python and Django

Before you can start learning Django you need to install some software on your computer. In this module I’ll show you how to install Python and Django and set up your development environment.

Installing Python and Django is a pretty simple process:

  1. First you need to Install Python;
  2. next you need to Install a Python Virtual Environment
  3. and finally you need to Install Django.

If this doesn’t sound familiar to you don’t worry. I’m assuming you have never installed software from the command prompt before and will lead you through it step by step.

I’ll be showing you how to install Python and Django on a Windows machine.

While there’s a strong Linux and macOS user base for Django most new users are on Windows. I also get lots of suggestions that we need more great tutorials on using Django on Windows so this is my answer to those suggestions.

It’s also easier for me because Windows has been my primary development platform for the last 20-odd years. Despite what you might hear to the contrary Windows is perfectly fine for developing web applications, you just don’t deploy them on Windows if you can help it.

If you are using Mac or Linux the installation process is not much different and once you are up and running in a Python virtual environment developing Django applications is pretty much identical on all three platforms.

If you are going to follow the course on a Mac or on Linux there are lots of tutorials on the Internet on how to install Django on these platforms the best place to start being Django’s own installation instructions.

For Windows users your computer can be running any recent version of Windows (7 8.1 or 10). I also assume you’re installing Django on a desktop or laptop computer and will be using the development server and SQLite to run all the code in the course. This is by far the easiest and best way to set up Django when you are first starting out.

Installing Python

Python is used by a lot of Windows applications so it’s possible that it’s already installed on your system. You can check this out by opening a command prompt or running PowerShell and typing python at the prompt.

If Python isn’t installed you’ll get a message saying that Windows can’t find Python.

If Python is installed, the python command will show the version of Python that’s installed on your computer and then open the Python interactive interpreter.

If Python is installed pay attention to the version number.

Django 2.1 and 2.2 are compatible with Python version 3.5 and later. If you have an older version of Python, you will have to install Python 3.7 for the code in this course to work.

If you have Python 3.5 or 3.6 I still recommend you install Python 3.7 to ensure you have the latest version installed on your machine.

Assuming Python 3.7 is not installed on your system you first need to get the installer. Search for Python in your browser and go to the download page and click the big yellow button that says “Download Python 3 point 7 point 0”.

At the time of writing the latest version of Python is 3.7.0 but it may have been updated by the time you read this so the numbers may be slightly different.

Once it’s downloaded run the Python installer. The installation process is the same as any other Windows program so if you’ve installed software before there should be no problem here; however there’s one really important customization you must make.

By default the Python executable is not added to the Windows PATH. For Django to work properly Python must be listed in the PATH statement.

Fortunately this is easy to do—when the Python installer screen opens make sure Add Python 3.7 to PATH is checked before installing.

Once Python is installed, restart windows and re-open the command window and type python at the command prompt. If Python installed correctly, the interactive interpreter will open, displaying Python 3.7 as the installed version of Python.

Installing a Python Virtual Environment

When you are developing new software with Python, it’s common to add and remove Python packages, modify dependencies and change system and environment variables.

By default, Python won’t keep all these changes separate to the main Python installation on your computer.

The problem with this is it’s almost certain that you will eventually modify some dependency or environment variable that another piece of software depends on.

A Python virtual environment solves this problem by wrapping all the dependencies and environment variables that your new software needs into a file system separate from the rest of the software on your computer.

There are a few different virtual environment tools available for Python. My favorite due to its simple setup and ease of use is virtualenv.

We install virtualenv from the command line using pip. Go ahead and enter pip install virtualenv at your command prompt and hit enter.

The output from your command window will either say that virtualenv is up to date, or pip will print out a list of messages as it installs the latest version of virtualenv.

The next step is to create a virtual environment for your project.

There are several schools of thought about where you should put your virtual environments. As they can live anywhere on your system, some developers like to keep all their virtual environments in one folder, others like to keep each virtual environment with the project it relates to.

I’m in the latter group. If you have several projects in development, I find it much easier to keep track of which virtual environment applies to what project by keeping them together in the project folder.

So, before we create our Python virtual environment we first need to create a project folder. The project folder can go anywhere on your computer although I recommend you create it somewhere in your user directory, so you don’t get any permission issues later on.

A good place for your folder in Windows is your My Documents folder. Create a new folder on your system, I have called the folder mfdw_project, because mfdw takes a whole lot less time to type than my first django website and its the project folder. You can give the folder any name that makes sense to you.

For the next step you need to be in PowerShell or at a command prompt inside your project folder. Don’t forget to use the handy shift-right click shortcut I showed you in the lesson on using the command line.

Once you are at the command prompt type virtualenv env_mfdw and hit enter.

Again, the name of the virtual environment is not important; you can change the name to suit your needs.

Once virtualenv has finished setting up your new virtual environment, open Windows Explorer and have a look at what virtualenv created for you. In your project directory you will now see a folder called \env_mfdw If you open the folder you will see a few different folders. If you look inside the \Lib folder, you’ll see virtualenv has created a complete Python installation for you, separate from your other software, so you can work on your project without affecting any of the other software on your system.

To use this new Python virtual environment we have to activate it, so let’s go back to the command prompt and type env_mfdw\scripts\activate and hit enter.

Oops! You can see by the video that something went wrong. I left this error in the lesson for a reason. If you’re using PowerShell for the first time, you are highly likely to get this error when you try and run the virtual environment.

The reason for the error is in the error message—running scripts in PowerShell is disabled by default.

This is easy to fix. Exit out of PowerShell and open it again as an administrator then type Set-ExecutionPolicy remoteSigned at the command prompt.

When PowerShell asks you if you want to make the changes, Enter A on your keyboard to say yes to all and then hit enter.

Close PowerShell and open it in your project folder again. Now when you enter the activation script it’ll run without the error and you’ll notice your command prompt has now changed:

(env_mfdw) C:\Users\...\mfdw_project>

The env_mfdw in brackets at the beginning of the command prompt lets you know that you’re running in the virtual environment.

Now that’s set up our next step is to install Django.

Installing Django

Now that we have Python installed and running a virtual environment, installing Django is super easy, just type pip install and then in inverted commas "django>=2.1,<2.2" and hit enter.

If you want to install Django 2.2, the setup is almost identical, except you type:

pip install "django>=2.2,<3" and hit enter.

This command tells pip to install the latest version of Django 2.1 (or 2.2) into your virtual environment.

To test whether the installation worked, go to your virtual environment and start the Python interactive interpreter by entering python at the command prompt.

If the installation was successful, you should be able to import the module django. Once Django is imported you can then call the get_version function to display the Django version.

You can see here I am running Django 2.1.

Don’t forget to exit the Python interpreter when you’re done.

Once you’ve installed Python and Django you can take the first step in developing a Django application by creating a Django project.

A Django project is a collection of settings and files for a single Django website. To create a new Django project we’ll be using a special command to auto-generate the folders, files and code that establishes a Django project. This includes a collection of settings for an instance of Django, database configuration, Django-specific options and application- specific settings.

I am assuming at this stage you are still running the virtual environment from the previous installation step. If not, you will have to start it again.

From your virtual environment command line, run the command:

django-admin startproject mfdw_site

This command will automatically create a new directory in your project directory as well as all the necessary files for a basic, but fully functioning Django website.

Next we need to create the database.

Django includes several applications by default (e.g. the admin program and user management. Some of these applications makes use of at least one database table, so we need to create a database and tables before we can use them.

To do this type cd mfdw_site at the command prompt to change into the mfdw_site folder created in the last step and run the command:

python manage.py migrate

The migrate command creates a new SQLite database and any necessary database tables according to the setup files created by the startproject command.

If all goes to plan, you’ll see a message for each migration it applies.

Now let’s run The Development Server to verify your Django project works. Change into the outer mysite directory and type python manage.py runserver and hit enter.

This will start the Django development server—a lightweight Web server written in Python. The development server was created so you can develop things rapidly, without having to deal with configuring a production server until you’re ready for deployment.

When the server starts Django will output a few messages before telling you that the development server is up and running at 127.0.0.1:8000. 127.0.0.1 is the IP address for local host, or your local computer. The :8000 is telling you that Django is listening at port 8000 on your local host.

You can actually change the port number if you want to but I have never found a good reason to change it, so best to keep it simple and leave it at the default.

Now that the server’s running, visit the development server’s IP address with your web browser. You’ll see Django’s default home page, complete with a cool animated rocket.

Woohoo! It worked! Give yourself a well-deserved pat on the back.