Skip main navigation

How to Install Python

In this step, you will install all the software and tools you need to code in Python and to start building programs.

In this step, you will install all the software and tools you need to code in Python and to start building programs.

 

How to check if Python is installed

 

First, let’s check to see if Python is installed on your computer and if it’s the correct version.

Open your command line/terminal. Depending on the type of operating system you have, how you access this will vary:

 

  • For Windows: Open the Command Line Prompt from the Windows toolbar.
  • For macOS: Select the Terminal icon from the bottom tray. If you don’t have it on your tray, use spotlight search (Command + space bar) and enter ‘Terminal’.
  • For Linux: In the graphical user interface console, select the Terminal icon on the desktop, or try Ctrl+Alt+T.

How to check what version of Python is installed

Execute the following command:

python --version

If Python is already installed, this command will show you the version of Python installed.

Screenshot of Windows Command Prompt

Next, check if your version is the most up-to-date. You can do this on the Python website[1] by selecting the Downloads tab, where the latest version of Python is available for download. Compare the version number with what you have.

 

Installing Python

 

If you do not have Python installed on your computer, or have an older version:

 

    1. Visit the Python website to find the most up-to-date and current source code, as well as binaries and documentation for all the supported platforms on the Python website.
    2. On the website, select the Downloads tab.

 

Screenshot of Python download page

 

In case you need Python for a different platform, scroll down the page for Windows, Mac, and Linux options.

 

Screenshot of Python release files
Click to enlarge

 

    1. To install Python, return to the main downloads page and select the gold ‘Download Python’ link. The process depends on your operating system: 
        • Windows: Download the Windows Installer and check Add Python to PATH.

       

        • Mac: Download the macOS installer.

       

        • Linux: Download the XZ compressed source tarball.

       

    1. Save the installer on your local machine, and then run it. This will bring up the Python installation wizard. Accept the default settings, and you are good to go! If you are using a Linux system, extract the files and check the README.rst file for instructions. Generally for Linux, you will need to open a terminal window in the extracted Python folder and run these commands one by one: 
        • ./configure

       

        • make

       

        • make test

       

        • sudo make install

       

       

 

If you are serious about developing as a Python programmer, consider trying out Linux as an operating system. Many online Python resources and guides assume that you are working in Linux. Many hosting solutions that you might end up using (such as Amazon Web Services) use Ubuntu Linux to run Python software and website backends in the cloud. As a result, it’s worth knowing something about this operating system. There is a tremendous variety of Linux distributions available and most of them are secure, free, and open source. You can also install them to run alongside the existing operating system on your device, especially if you usually use Windows.

 

If you are a complete Linux beginner, Ubuntu Linux [2] is probably the best option to start with.

 

When you have Python installed

 

Operating systems maintain path variables so that they can search specified directories for necessary executables and programs. This is useful when you need to work with a particular program or file in different directories, without you needing to specify the full location of the program each time.

 

 

A directory is a computer system cataloging structure that contains references to computer files, and possibly other directories. It’s the same thing as a ‘folder’ in many operating systems.

 

A file path specifies the unique address of a file or directory in a file system. A path is a slash-separated list of directory names followed by either a directory name or a file name. For example:

 

E:\Data\MyStuff (path terminating in a directory)

E:\Data\MyStuff\roads.shp (path terminating in a file)

 

In the case of Python, you need to add the information about the directory where Python has been installed to the PATH variable, so the Python interpreter can be invoked from any directory.

 

The path variable is named as:

 

    • path in Windows (not case sensitive for Windows)

 

    • PATH in Linux (case sensitive for Linux).

 

In the case of MacOS, the Python installer handles the path details for you.

 

Setting PATH in Windows

 

To add the Python directory to the path for a particular session in Windows, execute the following command prompt:

 

path %path%;C:\Python 

 

In this example, C:\Python is where the Python directory is located. If your Python directory is not located in C:\, you can permanently add this path by going to the environment variable setting and modifying the PATH variable in the system variable section.

 

This screenshot shows the process to modify your path in Windows.

 

Screenshot of the steps to set the Python path variable in Windows
Click to enlarge

 

Setting PATH in Linux

 

To add the Python directory to the path for a particular session in Linux, execute the following commands:

 

export PATH="$PATH:/usr/local/bin/python3"

 

Python provides a Python Shell (also known as Python Interactive Shell) that can execute a single Python command and get the result. Bash is the widely used shell, but you might be using a different one.

 

Here are commands for other shells (assuming /usr/local/bin/Python is the Python installation path):

 

In the csh shell, execute:

 

setenv PATH "$PATH:/usr/local/bin/python3"

 

In the sh or ksh shell, execute:

 

PATH="$PATH:/usr/local/bin/python3"

Code editors and IDEs

The next step is installing a code editor or an integrated development environment (IDE). These are software solutions for writing code. While code editors allow you to interact with and write and edit code, IDEs offer a wider range of features and allow you to edit source code, as well as build executables and debug your code. For example, an IDE may offer syntax highlighting, which can help you to easily differentiate between parts of your code, offer autocomplete suggestions on commands entered, and flag potential errors so that you can fix them before you attempt to execute the code.

Install

We recommend using PyCharm Community Edition to write and run your code. PyCharm offers a good combination of the benefits of IDEs and code editors. It will help you become familiar with these types of software. Download it here [3] free for your OS.

Set up

Once you have installed it, set up PyCharm for Python programming:

  1. Open PyCharm and click on New Project (square with +).
  2. A new tab will open with the name New Project.
  3. The Location will indicate where on your computer PyCharm is saving files and scripts. This is very important as you will need this information whenever you import files into Python.
  4. Activate New environment using and select Virtualenv from the dropdown menu.
  5. Click Create and PyCharm will prepare the workspace. This might take a minute or two depending on your operating system and computer specifications.
  6. The left window will have a list of all your Projects. The location where PyCharm will store your files and script will be shown underneath. You can move files up and down to reorganise them.
  7. PyCharm will open with a default main.py file. This file will be open in the right-hand side window with the icon displayed under Project in the left-hand side window. You can play around in this window. Note: you will notice dummy code at the top of this window – you need to clear this dummy code whenever you start a new project.
  8. Right-click on the first item under Project (left side window). This will be the directory where you store all the PyCharm files and scripts. Select New Python File, name the file, and press enter. Python files have .py extension.
  9. The name of this new Python file will be on top of the right-hand side window, just above the first code line. Your code snippets will appear in this window.
  10. You need to set the configuration before you can start to code. The top toolbar, on the right-hand side, has a window displaying Add Configuration. If something else appears here, click on the window to open the tab. Click on the negative sign (-) to remove the current configuration. The default setting is Add Configuration.
  11. Next, click on File – Settings – Project: (your directory) – Python Interpreter. Select Python 3.9 or later from the dropdown menu. Click OK. If there is no Python version to select, you did not install Python correctly. Go back to the instructions on installing Python.
  12. PyCharm has a very good Help function in the top toolbar. This consists of videos, blogs, examples, and sandboxes that are regularly updated by the developers.
  13. You are now ready to start coding.

For another option, you can use another IDE or code editor instead of PyCharm. If you can’t or don’t want to install any of these, you can use an online Python interpreter, although it may not have the full capability to complete the assignments and coding activities in this course.

 

References

  1. Welcome to Python.org [Internet]. Python.org; [date unknown]. Available from: https://www.python.org/
  2. Enterprise Open Source and Linux [Internet]. Ubuntu; [date unknown]. Available from: https://ubuntu.com/
  3. Download PyCharm: Python IDE for Professional Developers by JetBrains [Internet]. JetBrains; [date unknown]. Available from: https://www.jetbrains.com/pycharm/download/
This article is from the free online

Introduction to Programming with Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now