Technology. Explained.

Programming, Python Tutorial, Series

Python tutorial 02: install Python

Python code

In the previous tutorial we looked at the main features of the Python language, now it’s time to take the next step and install Python. At the end of this tutorial you will be able to install Python whether you are running Windows, OS X or Linux. After the installation you will also be able to check if everything is working fine on your system by running your first Python script! We will start by checking if Python is already installed on your system (whichever operating system you are running), then we will see the actual Python installation process and (last but not least), we will look at how to install a good text editor. After that, the fun part will begin!

Before we jump to the actual Python installation, though, we would like to spend a few words about Python support, since you will certainly find it useful in the future.

Python support

Python support has been cited among Python’s key features in the previous post. Once you install Python and start coding, you will have to deal with errors, or you won’t be able to do what you would like to do. That’s perfectly fine! Even people who spent an entire life coding often do the same. The important thing is knowing that you will always have a supportive community ready to answer any of your questions.

Python 2 vs Python 3

At the time of writing there are two versions of Python that you may download, these are Python 2 and Python 3. Even though you won’t notice many differences in the beginning, some code written for Python 2 might not work in Python 3. If you wish to examine the differences between the two versions, they are well explained at the following link. We strongly suggest to install Python 3, since it is the most recent version as well as the one we will be using in this Python tutorial. We will point out the main differences throughout this tutorial, so you should be able to follow along regardless of the version you are running.

Install Python

We have created three different sections for this purpose, so that you can easily follow the installation procedure based on the operating system you are running.

Install Python on Windows

Windows logo

In this section we will see how to check if Python is already installed on your system, how to install Python if necessary and, finally, how to install a text editor.

Checking if Python is installed

If you are running Windows, chances are you will need to download Python and install it, since it usually doesn’t come with the operating system.

First of all open the Start menu and enter command in the text field, this will open a Command window (as an alternative, you can press the combination Win+R to open the Run window and enter cmd). In the command prompt you have just opened, type (all lowercase):

and press Enter. If you see an error message (as it is likely to happen), Python is not installed on your computer. If, instead, you get a result with the last line being the Python prompt:

then Python is installed on your computer. If so, you can skip the next paragraph and jump to the Text Editor section.

Install Python – Python installation on Windows

To install Python just head over to the official Python website and download the Python installer by clicking on one of the two buttons. You should be able to choose either Python 2 or Python 3 (at the time of writing, the two most recent versions are 2.7.13 and 3.6.0). Proceed downloading Python 3. The installation procedure should be pretty self-explanatory, just run the file you have downloaded and proceed with the installation. Important note: one thing you should remember to do while installing Python is checking the option “Add Python 3.X to PATH“, as this will make things easier later.

At this point everything should be fine and Python should be installed on Windows. Let’s check if everything is working fine. Open a command window again (just like you did before) and enter the word python (all lowercase, once again). If the last line you see is:

Python is working! At this point you can enter the following text in the command prompt:

You should get this output:

Awesome, you just ran a very short Python program (you told Python to print the sentence “Hello, world!” to the console). To quit the Python terminal session, just run:

Install a good text editor for Python

The importance of having a good text editor is, unfortunately, often underrated. Yes, some people are even used to write code in the Notepad, but it is not the most convenient way of coding. A nice text editor will help you in many ways (it will, for example, color and highlight your code so that you can easily distinguish the different blocks of code and what they do).

The list of Python editors is endless: if you wish to try a few of them before sticking to the one that suits you best, you can take a look at the Python Wiki. What we suggest (which is, indeed, the one we use) is Sublime Text. If you take a look at the license, you should find the following: “Sublime Text may be downloaded and evaluated for free, however a license must be purchased for continued use. There is no enforced time limit for the evaluation“. This means that you are never forced to buy it and could virtually run it for free forever. However, if you like it, we strongly encourage you to support the author’s work and buy it.

From the official website (link above), click on Download on the top bar and download the installer for your operating system. The installation procedure is straight-forward, you just need to run the installer you have downloaded and follow the steps indicated.

The last thing you need to do is tell Sublime Text where to find Python 3. To do so, open Sublime Text and go to Tools > Build System > New Build System. Delete everything you see and enter, instead:

Where Python3x-32 depends on the version you have installed (if you installed Python 3.6, it will be Python36-32. This is assuming that you installed Python in the default path. If you didn’t, you will have to substitute the path above with the one pointing to the actual directory (this is why you should have checked the option “Add Python 3.X to PATH” during Python’s installation). Now Save the file with the name Python3.sublime-build to the default directory (the one that Sublime Text will save the file to by default). If you now go to Tools > Build System, you should see a Python3 option which you will use for running files with Python 3.

You are all set to go and ready to write your first Python script!

Install Python on OS X

OS X logo

In this section we will see how to check if Python is already installed on your system and how to install a text editor.

Making sure that Python is installed

Unlike Windows, OS X should come with Python already installed. To make sure that your system already has Python installed, open the Terminal (Applications > Other folder, or you can press Cmd+Spacebar, type terminal and press Enter).  In the Terminal window you have just opened, type (lowercase):

and press Enter. You should get an output telling you what Python version is installed, with the last line being the following Python prompt:

This means that Python is already installed on your system! Note that the version mentioned in the output is, usually, 2.x.x. To check whether Python 3 is installed or not, enter python3 instead of just python. You should get an output similar to the one above. Next step is to install a nice text editor and you will be ready to code.

Install a good text editor for Python

Like we mentioned above when talking about Windows text editors, a good text editor is probably the best way to save time when coding. Colors make it easier to distinguish the different code sections and, at the same time, a good text editor makes the code more clean and readable. This means also that you will find it easier to correct errors. A long list like the one of the Python Wiki can give you an idea of how many text editors are available out there. What we suggest though is Sublime Text. If you take a look at the license, you should find the following: “Sublime Text may be downloaded and evaluated for free, however a license must be purchased for continued use. There is no enforced time limit for the evaluation“. This means that you will never be forced to buy it and could virtually run it for free forever. However, if you like it, we strongly encourage you to support the author’s work and buy it.

From the official website, just click on Download on the top bar and download the .dmg installer for OS X. The installation procedure is fairly straight-forward: the only thing you need to do is run the installer you have downloaded and follow the steps indicated.

Note that if you force the use of a specific Python version (for example you start your Python session by running python3 instead of just python), you need to tell Sublime Text where to find the correct directory. This happens because the default Python version that will be launched is Python 2, even if Python 3 is installed. To make sure that everything will work fine throughout this course, open Sublime Text and go to Tools > Build System > New Build System. Delete everything you see and enter, instead:

At this point just Save the file with the name Python3.sublime-build to the default directory (the one that Sublime Text will save the file to by default).

You are now ready to write your first Python script!

Install Python on Linux

Linux logo

In this section we will see how to check if Python is already installed on your system and how to install a nice text editor for Python.

Making sure that Python is installed

Python is already installed in most computers running Linux. You can easily check what version of Python is installed on your computer by running the Terminal and writing:

You should see an output telling you which Python’s version is installed on your computer; something like:

In this case, the default version of Python installed is 2.7.13. You can then exit from the Python prompt and go back to the Terminal.

In a similar way, you can check if Python 3 is installed on your computer by running the command:

Note that if you previously received an output like the one above, it doesn’t necessarily mean that Python 2.7.13 is the only version installed; it just means that Python 2 is the default version. So after running the command python3 you may get an output very similar to the one above, but with one of the lines reporting something like

In case you receive a similar output, Python 3 is installed on your computer, too.

Install a good text editor for Python

Even though the text editor we suggested for Windows and OS X was Sublime Text, we encourage you to install Geany if you are on a Linux system: it will nicely highlight your code and also allows to run scripts from the editor, without launching the Terminal. Plus, “Geany is distributed under the terms of the GNU General Public License as published by the Free Software Foundation”, to cite the official website. Sublime Text is available for Ubuntu (you can find it on the official website) or, as a tarball, for other Linux versions (same link).

All you have to do in order to install Geany on Linux is entering this command in a Terminal window:

That’s it! You are now ready to run some Python code!

 

Python tutorial: logo

Run Python script

At this point you should have made sure that Python 3 is installed on your system and you should also have a brand-new text editor installed on your computer. This means two things:

  1. The boring part is over (unless you found the previous steps exciting, which we doubt)
  2. You can now run your first Python script!

First of all, whichever operating system you are running, you should create a folder where you will save all of your Python programs presented in this course. A good habit is to use lowercase characters when naming folders. If your folder’s name is longer than one word, separate the words with an underscore character. During the course we will assume you have a folder on your Desktop named python_code. Of course you can name your folder in other ways and place it wherever you prefer, just remember you will have to enter a different path than the one presented here.

Running a Python script on Windows

Windows logo

Running hello_world.py from the text editor

To create and run a Python script in Sublime Text follow these easy steps:

  1. Open Sublime Text
  2. In a new file, enter the following code:
  3. Go to File > Save As and name the file hello_world.py. Save it in the python_code folder you created before
  4. Select Tools > Build System > Python3 to set Python 3 as the default version
  5. Now select Tools > Build
  6. You should see the program’s output at the bottom, something like:

You just launched your first Python program! 🙂 Let’s see how to do the same from Terminal.

Running hello_world.py from Terminal

  1. After you created the hello_world.py file (steps 1 to 3 above), open a Terminal window
  2. Enter:
    This will change directory (cd) so that it points to the python_code folder
  3. Now enter:
    to run the program you have just written
  4. The output should be:

Let’s see what the next steps on Windows will be!

Running a Python script on Mac OS X

OS X logo

Running hello_world.py from the text editor

Creating and running a Python script in Sublime Text is very easy:

  1. Open Sublime Text
  2. In a new file, enter the following code:
  3. Go to File > Save As and name the file hello_world.py. Now save it in the python_code folder you created before
  4. Select Tools > Build System > Python3 to set Python 3 as the default version
  5. Now select Tools > Build
  6. You should see the program’s output in a Terminal window like the following:

Congratulations! You just launched your first Python program! Let’s see how to do the same from Terminal.

Running hello_world.py from Terminal

You can run a Python program from the Terminal, too. After you followed steps 1 to 3 above to create the hello_world.py file:

  1. Open a Terminal window
  2. Enter:
    in order to navigate to the python_code folder (cd stands for “change directory”)
  3. Now enter:
  4. You should see the:
    output as before.

Let’s see what the next steps on Mac OS X will be!

Running a Python script on Linux

Linux logo

Running hello_world.py from the text editor

  1. Open Geany
  2. In a new file, enter the following code:
  3. Go to File > Save As and name the file hello_world.py. You can then save it in the python_code folder you created before
  4. Now go to Build > Set Build Commands. If you want to use Python 3, you need to change the commands next to Compile and Execute. Enter exactly
    python3 -m py_compile “%f” next to Compile and
    python3 “%f” next to Execute.
  5. Finally, select Build > Execute or click on the Execute icon.
  6. You should see the program’s output in a Terminal window, like the following:

Congratulations! You just launched your first Python program! Let’s see how to do the same from Terminal.

Running hello_world.py from Terminal

You can run a Python program even from the Terminal, after you saved the file. So, after you followed steps 1 to 3 above to create the hello_world.py file:

  1. Open a Terminal window
  2. Enter:
    in order to navigate to the python_code folder (cd stands for “change directory”)
  3. Now enter:
  4. You should see the:
    output as before.

Awesome! Let’s see what is up next.

Next steps

First of all congratulations! This was a long tutorial, but you learned a lot of stuff: you checked if Python was installed on your system (and maybe installed it), configured a text editor and even wrote your first program. After that, you launched it both from the text editor and the Terminal window.

From now on, things will be much more fun and interesting, as you will learn how to create variables, lists, conditional instructions, iterations and much much more! With those instruments, you will be able to write virtually any program you can think of.

Our next step is, as mentioned, to learn how to use variables in Python.

If you liked this tutorial and found it helpful, let everyone know by sharing it! For any doubt or question you may have, leave a comment: we’ll be more than happy to help you! 🙂

Leave a Reply