Lesson Contents
In this lesson, we’ll take a look at how we can install Python. Before January 1, 2020, there were two Python versions:
- Python 2.x
- Python 3.x
Python 2.0 was released in 2000 and the latest version, 2.7, was released in 2010. Python 3.0 is from 2008. Python 2.x and 3.x are very similar but there are some (syntax) differences. Because Python 2.x is older, there is still a lot of code out there written for Python 2.x.
Since January 1, 2020, Python 2.7 is retired. When you download Python, it will be the latest 3.x version.
Installation
Let’s see how we can install Python on different operating systems.
Linux and Mac OS X
Linux and Mac OS are easy because Python is pre-installed on Mac OS X and most Linux distributions. You can check the Python version from the command line. For example, let’s see what Ubuntu 18.04 LTS has pre-installed:
python --version
Python 2.7.17
python3 --version
Python 3.6.9
Ubuntu 18.04 LTS comes with Python 2.7 and 3.6.9 pre-installed.
If you want the latest version on Mac OS X, you can always grab it from Python.org MAC OS X downloads. For Linux, upgrading Python depends on your Linux distribution.
Windows
Microsoft Windows 10 doesn’t come with Python pre-installed, so we’ll have to install it ourselves. Head over to Python.org downloads and download the installation file:
Open the installation file and you’ll see the following screen:
The installation is straightforward but I would suggest clicking on the “Add Python 3.x to PATH” checkbox. This means you can run Python anywhere from the command line, not just from the folder where it is installed. Hit the Install Now button and you only have to wait a minute for the installation to complete:
Once the installation is successful, click on Close and we are done:
In your start menu, you will find some new items:
This completes our installation.
Verification
There are two ways to run Python:
-
- Integrated Development and Learning Environment (IDLE): Python’s built-in Integrated Development Environment (IDE).
- CLI: You can start Python from the command line.
Let’s try both options.
IDLE
Click on the IDLE icon in your Windows 10 start menu:
You’ll see a white screen with a Python shell:
The >>>
that you see above, is the prompt of the interactive Python interpreter. This means you can type Python code and the interpreter will execute it right away. Here is a quick example:
>>> print("hello world")
hello world
You can also save Python files and execute them later. Click on “New File” in the “File” menu:
You’ll get a blank screen where you can type your Python code:
Click on “Save” or “Save As” in the “File” menu:
Enter your filename. I’ll go for “hello-world.py”:
Now you can click on “Run Module” under the “Run” menu to execute your code:
You will see the output of your code in the Python shell:
That’s how we execute Python code.
CLI
You can also start the Python interpreter from the CLI:
C:\>Python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Let’s run some code:
>>> print("hello world")
hello world
If you want to exit, type exit()
:
>>> exit()
C:\>
You can also run a Python file directly. For example, let’s try to run the “hello-world.py” file we created with IDLE:
C:\>python hello-world.py
hello world
That’s how we can use the CLI to run Python code.
Trinket.io
To explain Python, I could use “static” code examples as I did above and show the output. There is a better way though. In all the upcoming Python lessons, I use trinket.io for code. With trinket.io I can share code, and you can run it interactively from this website without having to install anything. Trinket.io runs Python through Javascript on your own computer. You can see immediately what the code does and even edit it if you want to try something yourself.
With trinket.io we have two options to run our Python code, similar to how you run code on your own computer:
- Start the interactive Python interpreter.
- Save and run .py files.
I use both options in our lessons. Here is an example of the Python interpreter which executes our code automatically:
In the trinket above, you can see the >_
button. This means we use the Python interpreter. Below is an example where we use a .py file. You need to click on the >
button to execute the code:
In the trinket above, the >
button means we execute a .py file. You can move the separation bar between the two panes to see more of the code or the output if needed.
>_
or >
button to re-run your code.Conclusion
We learned a few things about Python:
- How to verify your pre-installed Python version on Mac OS X or Linux.
- How to install Python on Microsoft Windows.
- How to run code using Python’s built-in IDE (IDLE).
- How to run code from the CLI with the Python interpreter.
We are now ready to dive further into Python.
Hi Rene,
Today, I was discussing with the network administrator of my company about thenetwork programmability and Devops.
He told me that Ansible will replace python for network automation, because it is more easier to learn and code for a lot of network engenieers.
What do you think about that?
Thanks as always
Hello Giovanni
It is important to understand the role and the functionality of both Ansible and Python in order to determine which is best to use, and in which situations.
Ansible and Python are not direct equivalents, and cannot be directly compared. Ansible is a tool that is written in Python, and you don’t need to know any Python to use it. But, if you do know Python, then it is easy to extend the capabilities and functionalities of Ansible. As such, Ansible relies on Python to function.
Ansible is easier to use, and easier to learn, as you correctly stat
... Continue reading in our forumThank you
Hi Laz,
“Add Python 3.x to PATH” checkbox. This means you can run Python anywhere from the command line, not just from the folder where it is installed" , here i don’t get "you can run anywhere’’ mean at any computer in a n/w from single computer if it is installed on each and every computer?
Hello Pradyumna
This simply means that you can run it from any PATH in the command prompt. If the checkbox is not checked, then you must be in the same folder as the python installation files.
For example, if you don’t check the box, then you must be in the following folder in the CLI in order to run python:
https://cdn-forum.networklessons.com/uploads/default/original/2X/2/2d145e50b450cfdef12b4904b5da5d7affd0f83e.png
If you check the box, then you can run from any location in the CLI such as:
C:\Users\user
or even
C:\
https://cdn-forum.networklessons.com/uplo
... Continue reading in our forum