Chapter 1: Initial Set Up
This chapter covers how to properly configure your Windows or macOS computer to work on Django projects. It is similar to the set up guides in both Django for Beginners and Django for APIs so if you are already familiar with the command line, have the latest version of Python installed, know how to create virtual environments, and use Git you can consider this chapter optional. Otherwise read through to make sure your computer is ready for professional Django development and success in the rest of this book.
The Command Line
The command line is a text-only interface that is used frequently by developers. It harkens back to the early days of computer interfaces before the mouse and graphical user interfaces became the norm. In practice, multiple terms are used to refer to the command line: Command Line Interface (CLI), console, terminal, shell, or prompt. Technically speaking, the terminal is the program that opens up a new window to access the command line, a console is a text-based application, the shell is the program that runs commands on the underlying operating system, and the prompt is where commands are typed and run. It is easy to be confused by these terms initially but they all essentially mean the same thing: the command line is where we run and execute text-only commands on our computer.
On Windows, the built-in terminal and shell are both called PowerShell. To access it, locate the taskbar on the bottom of the screen next to the Windows button and type in “powershell” to launch the app. It will open a new window with a dark blue background and a blinking cursor after the >
prompt. Here is how it looks on my computer.
PS C:\Users\wsv>
On macOS, the built-in terminal is called appropriately enough Terminal. It can be opened via Spotlight: press the Command
and space bar
keys at the same time and then type in “terminal.” Alternatively, open a new Finder window, navigate to the Applications directory, scroll down to open the Utilities directory, and double-click the application called Terminal. This opens a new screen with a white background by default and a blinking cursor after the %
prompt. Don’t worry about what comes to the left of the %
prompt. It varies by computer and can be customized later on.
Wills-Macbook-Pro:~ wsv%
To avoid confusion around the different prompts for each operating system–>
on Windows and %
on macOS–in this book we will use the Unix prompt of $
for any command line usage. Where there is a difference between Windows and macOS both commands will be listed.
There are many available shell commands but most developers rely on the same handful over and over again and look up more complicated ones as needed. In most cases, the commands for Windows (PowerShell) and macOS are similar. For example, the command whoami
returns the computer name/username on Windows and just the username on macOS. As with all shell commands, type the command itself followed by the return
key. Note that the #
symbol represents a comment and will not be executed on the command line.
# Windows
$ whoami
wsv2022/wsv
# macOS
$ whoami
wsv
Sometimes, however, the shell commands on Windows and macOS are completely different. A good example is the command for outputting a basic “Hello, World!” message to the console. On Windows the command is Write-Host
while on macOS the command is echo
.
# Windows
$ Write-Host "Hello, World!"
Hello, World!
# macOS
$ echo "Hello, World!"
Hello, World!
Most Django developers are not command line experts and rely on the same handful of commands over and over again. In this book I will provide the exact instructions to run each time but if you are curious, a complete list of shell commands for each operating system can be found over at ss64.com
.
Install Python 3
Make sure that the latest version of Python (3.10 as of this writing) is available on your computer.
On Windows, Microsoft hosts a community release of Python 3 in the Microsoft Store. In the search bar on the bottom of your screen type in “python” and click on the best match result. This will automatically launch Python 3.10 on the Microsoft Store. Click on the blue “Get” button to download it.
To confirm Python was installed correctly, open a new Terminal window with PowerShell and then type python --version
.
$ python --version
Python 3.10.4
The result should be at least Python 3.10. Then type python
to open the Python interpreter from the command line shell.
$ python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 19:00:18)
[MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits", or "license" for more information.
>>>
On Mac, the official installer on the Python website is the best approach. In a new browser window go the Python downloads page and click on the button underneath the text “Download the latest version for Mac OS X.” As of this writing, that is Python 3.10. The package will be in your Downloads
directory. Double click on it which launches the Python Installer and follow through the prompts.
To confirm the download was successful, open up a new Terminal window and type python3 --version
.
$ python3 --version
Python 3.10.4
The result should be at least 3.10. Then type python3
to open the Python interpreter.
$ python3
Python 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Text Editor
The command line is where we execute commands for our programs but a text editor is where actual code is written. The computer doesn’t care what text editor you use–the end result is just code–but a good text editor can provide helpful hints and catch typos for you.
There are many modern text editors available but a very popular one is Visual Studio Code, which is free, easy to install, and enjoys widespread popularity. If you’re not already using a text editor, download and install VSCode from the official website.
An optional–but highly recommended–additional step is to take advance of the large ecosystem of extensions available on VSCode. On Windows, navigate to File -> Preferences -> Extensions
or on macOS Code -> Preferences -> Extensions
. This launches a search bar for the extensions marketplace. Enter “python” which will bring up the Microsoft extension as the first result. Install it.
A second extension to add is Black, which is a Python code formatter that has quickly become the default within the Python community. To install Black, open a Terminal window within VSCode by going to Terminal -> New Terminal
at the top of the page. In the new terminal window opened at the bottom of the page, type python -m pip install black
. Next, open up the VSCode settings by navigating to File -> Preferences -> Settings
on Windows or Code -> Preferences -> Settings
on macOS. Search for “python formatting provider” and select black
from the dropdown options. Then search for “format on save” and enable “Editor: Format on Save”. Black will now automatically format your code whenever a *.py
file is saved.
To confirm this is working, use your text editor to create a new file called hello.py
within the ch1-setup
directory. located on your Desktop and type in the following using single quotes:
print('Hello, World!')
On save, it should be automatically updated to using double quotes which is Black’s default preference: print("Hello, World!")
. That means everything is working properly.
Install Git
The final step is to install Git, a version control system that is indispensable to modern software development. With Git you can collaborate with other developers, track all your work via commits, and revert to any previous version of your code even if you accidentally delete something important!
On Windows, navigate to the official website at https://git-scm.com/
and click on the “Download” link which should install the proper version for your computer. Save the file and then open your Downloads folder and double click on the file. This will launch the Git for Windows installer. Click the “Next” button through most of the early defaults as they are fine and can always be updated later as needed. There are two exceptions however: under “Choosing the default editor used by Git” select VS Code not Vim. And in the section on “Adjusting the name of the initial branch in new repositories” select the option to use “main” as opposed to “master” as the default branch name. Otherwise the recommended defaults are fine and can always be tweaked later if needed.
To confirm Git is installed on Windows, close all current shell windows and then open a new one which will load the changes to our PATH variable. Type in git --version
which should show it is installed.
# Windows
$ git --version
git version 2.35.1.windows.2
On macOS, installing Git via Xcode is currently the easiest option. To check if Git is already installed on your computer, type git --version
in a new terminal window.
# macOS
$ git --version
If you do not have Git installed, a popup message will ask if you want to install it as part of “command line developer tools.” Select “Install” which will load Xcode and its command line tools package. Or if you do not see the message for some reason, type xcode-select --install
instead to install Xcode directly.
Be aware that Xcode is a very large package so the initial download may take some time. Xcode is primarily designed for building iOS apps but also includes many developer features need on macOS. Once the download is complete close all existing terminal shells, open a new window, and type in git --version
to confirm the install worked.
# macOS
$ git --version
git version 2.32.0 (Apple Git-132)
Once Git is installed on your machine we need to do a one-time system configuration by declaring the name and email address associated with all your Git commits. We will also set the default branch name to main
. Within the command line shell type the following two lines. Make sure to update them your name and email address.
$ git config --global user.name "Your Name"
$ git config --global user.email "yourname@email.com"
$ git config --global init.defaultBranch main
You can always change these configs later if you desire by retyping the same commands with a new name or email address.
Conclusion
Django is an excellent choice for any developer who wants to build modern, robust web applications with a minimal amount of code. While configuring a software development environment is no easy task you should now have Python 3.10 installed, Git installed, and configured a modern text editor.
In the next chapter we’ll learn about Docker and how to configure any computer for Django development with Docker.