Coders' Guidebook

How to install Python and establish a virtual environment

Python is a programming language that can be used to develop applications. Some computers have Python pre-installed. If you use a Windows PC then you can check whether Python is already installed by opening up the 'Command Prompt' app. The easiest way to open Command Prompt is to press the Windows and R keys on your keyboard then type 'cmd' into the Run window.

Once Command Prompt is open type:

python --version

Linux users can check whether Python is installed by entering the above statement into the command line, while Mac users can type the statement into the Terminal.

If Python is installed then the python --version command will result in output similar to the following:

Python 3.8.0

The above output would show that Python release 3.8.0 is currently installed. By the time you read this, it is possible newer versions of Python may be available.

Install Python and Pip

If Python is not already installed then you can download it here. When installing Python you may be prompted to install Pip as well. If so, then be sure to accept this because pip is a package manager for Python that will come in handy later.

To check whether Pip is installed on your computer then open Command Prompt (or equivalent) like before and type:

pip --version

If Pip is installed then you will get an output similar to the following:

pip 19.2.3 from c:\users\User name\appdata\local\programs\python\python38-32\lib\site-packages\pip (python 3.8)

That shows Pip release 19.2.3 is currently installed. If Pip is not installed and you have to add it manually, then first download this get-pip.py file. For simplicity, save it in the folder that Command Prompt reverts to by default (C:\Users\Your user name). In Command Prompt you can then instruct Python to run the file and install Pip by typing this command:

python get-pip.py

Once Python and Pip are installed on your computer then we can begin laying the groundwork for our first project.

Working directories for Python and Django projects

Before building an application using Python or Django, you should first create a folder that will serve as the working directory (the place on your computer where the application will be built and stored). To set the working directory, open up the 'Command Prompt' app (or equivalent) and type 'cd' (change directory) followed by the location of your chosen folder. You can either input the location relative to the current directory (most often C:\Users\Username by default) or type the full location beginning with C:\. For example, if you created a folder called 'first_website' inside the Documents folder of your computer then you could set it as the working directory by typing either of the following commands:

cd documents\first_website
cd C:\Users\Username\Documents\first_website

You will know it has worked because new lines in the Command Prompt should now begin like this:

C:\Users\Username\Documents\first_website>

Instead of:

C:\Users\Username>

Virtual environments

With the working directory now set, we next need to establish a virtual environment. Virtual environments link the Python packages and components that are used in a given project and allow them to work together. Each virtual environment is isolated from one another and so allows different Python package configurations to be used across different projects.

To create a virtual environment we will need to use virtualenv. You can check whether virtualenv is already installed by typing the following into Command Prompt:

virtualenv --version

If virtualenv is not recognised as a command then you can install it by typing the following instruction into Command Prompt:

pip install virtualenv

Command Prompt will then download and install virtualenv. The message 'Successfully installed virtualenv-16.7.8' will display when complete (although the version number may vary).

Input the following command to create a virtual environment:

virtualenv env

You will know the virtual environment has been established when the following message is displayed:

Installing setuptools, pip, wheel...
done.

Once a virtual environment has been created it then needs to be activated. This is achieved by directing Command Prompt to a file called activate.bat that was created when the virtual environment was established:

Windows users

env\Scripts\activate

Mac/Linux users

source env\bin\activate

When the virtual environment is active, you may notice the working directory in Command Prompt is now preceded by (env):

(env) C:\Users\Username\Documents\first_website>

Should you ever need to exit the virtual environment, simply enter the following command:

deactivate

Once your virtual environment is set up successfully then you are ready to get stuck in using Django!

<<< Previous

Next >>>