Installation
Part of this section is taken from Django Girls Tutorial: https://tutorial.djangogirls.org/en/installation/
Set up virtualenv and install Django
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 thevirtualenv
; your home directory, for example. On Windows, it might look likeC:\Users\Name\
(whereName
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:\codinggrace
For this tutorial we will be using a new directory textbasedgame
in your home directory:
$ mkdir textbasedgame
$ cd textbasedgame
We will make a virtualenv calledmyvenv
. The general command will be in the format:
python3 -m venv myvenv
Virtual environment: Windows
To create a newvirtualenv
, you need to open the command prompt and runpython -m venv myvenv
. It will look like this:
C:\Users\Name\textbasedgame
> python -m venv myvenv
Wheremyvenv
is the name of yourvirtualenv
. You can use any other name, but stick to lowercase and use no spaces, accents or special characters. It is also 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:
$ 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!
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 directory and files).
Working with virtualenv: Windows
Start your virtual environment by running:
C:\Users\Name\textbasedgame
> 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:
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
Working with virtualenv: Linux and OS X
Start your virtual environment by running:
$ source myvenv/bin/activate
Remember to replace myvenv
with your chosen virtualenv
name!
NOTE: Sometimes
source
might not be available. In those cases try doing this instead:$ . 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:
(myvenv) ~$ pip install --upgrade pip
Then run pip install django
.
$ pip install django
Collecting django
Using cached Django-2.0.1-py3-none-any.whl
Collecting pytz (from django)
Using cached pytz-2017.3-py2.py3-none-any.whl
Installing collected packages: pytz, django
Successfully installed django-2.0.1 pytz-2017.3
Installing Django: Windows
If you get an error when calling pip on Windows platform, please check if your project pathname contains spaces, accents or special characters (for example,
C:\Users\User Name\textbasedgame
). If it does, please consider using another place without spaces, accents or special characters (suggestion:C:\textbasedgame
).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 after when you try to install Django. If this happens, instead of the above command use:
C:\Users\Name\textbasedgame> python -m pip install django
Installing Django: Linux
If you get an error when calling pip on Ubuntu 12.04 please run the following to fix the pip installation in the virtualenv.
$ python -m pip install -U --force-reinstall pip
That's it! You're now (finally) ready to create a Django application!
Code Editors
Following editors in order of recommendation:-
- VSCode (Recommended) - https://code.visualstudio.com
- Sublime Text 3 - https://www.sublimetext.com/3
- PyCharm - https://www.jetbrains.com/pycharm
- Atom - https://atom.io
- Gedit - https://wiki.gnome.org/Apps/Gedit#Download
Why are we installing a code editor?
You might be wondering why we are installing this special code editor software, rather than using something like Word or Notepad.
The first reason is that code needs to be plain text, and the problem with programs like Word and Textedit is that they don't actually produce plain text, they produce rich text (with fonts and formatting), using custom formats like RTF (Rich Text Format).
The second reason is that code editors are specialized for editing code, so they can provide helpful features like highlighting code with color according to its meaning, or automatically closing quotes for you.
We'll see all this in action later. Soon, you'll come to think of your trusty old code editor as one of your favourite tools. :)
Install Git
Git is a "version control system" used by a lot of programmers. This software can track changes to files over time so that you can recall specific versions later. A bit like the "track changes" feature in Microsoft Word, but much more powerful.
Installing Git: Windows
You can download Git from git-scm.com. You can hit "next" on all steps except for one; in the fifth step entitled "Adjusting your PATH environment", choose "Use Git and optional Unix tools from the Windows Command Prompt" (the bottom option). Other than that, the defaults are fine. Checkout Windows-style, commit Unix-style line endings is good.
Do not forget to restart the command prompt or powershell after the installation finished successfully.
Installing Git: OS X
Download Git from git-scm.com and just follow the instructions.
Note
If you are running OS X 10.6, 10.7, or 10.8, you will need to install the version of git from here:
Installing Git: Debian or Ubuntu
$ sudo apt-get install git
Installing Git: Fedora
$ sudo dnf install git
Installing Git: openSUSE
$ sudo zypper install git
Create a GitHub account
Go to GitHub.com and sign up for a new, free user account.