Python has never been easy to install. In order to proceed, let's make sure that we have set up Python on our machine. We will see how to use Python on macOS or Linux and how to install it on Windows.
Installing and setting up Python
Using Python on Mac or Linux
On a macOS or Linux system, you do not need to install Python because it is already included. You just need to open a Terminal window and enter the python command. This will put you in an interactive mode where you can execute python commands one by one. You can close the interactive mode by executing the exit() command. So, basically, to create a script, we use the nano text editor followed by the name of the file. We then enter python commands and save the file. You can then run the script with python followed by the script name. So, let's see how to use Python on macOS or Linux in the following steps:
- Open the Terminal on a macOS or Linux system and run the python command. This opens an interactive mode of Python, as shown in the following screenshot:
- When you use the print command, it prints Hello right away:
>>> print "Hello"
Hello
- We will then leave with the following command:
>>> exit()
- As mentioned before, to use Python in interactive mode, we will enter the command as shown:
$ nano hello.py
- In the hello.py file, we can write commands like this:
print "HELLO"
- Save the file by pressing Ctrl + X followed by Y and Enter only if you've modified it.
- Now, let's type Python followed by the the script name:
$ python hello.py
When you run it, you will get the following output:
The preceding command runs the script and prints out HELLO; that's all you have to do if you have a macOS or Linux system.
Installing Python on Windows
If you have Windows, you have to download and install Python.
Here are the steps which you need to follow:
- Download Python from https://www.python.org/downloads/
- Run it in a Command Prompt window
- Start interactive mode with Python
- Close with exit()
To create a script, you just use Notepad, enter the text, save the file with Ctrl + S, and then run it with python followed by the script name. Let's get started with the installation.Â
Open the Python page using link given previously and download Python. It offers you various versions of Python. In this book, we will use Python 2.7.12.
Sometimes, you can't install it right away because Windows marks it as untrusted:
- You have to unblock it in the properties first so that it will run, and run the installer
- If you go through the steps of the installer, you'll see an optional step named Add python.exe to path. You need to choose that selection
The purpose of that selection is to make it so Python can run from the command line in a Terminal window, which is called Command Prompt on Windows.
Now let's proceed with our installation:
- Open the Terminal and type the following command:
$ python
- When you run it, you can see that it works. So, now we will type a command:
print "HELLO"
Refer to the following screenshot:
- We can exit using the exit() command as shown earlier.
- Now, if we want to make a script, we type the following command:
notepad hello.py
- This opens up Notepad:
- We want to create a file. In that file, we enter the following command:
print "HELLO"
- Then, save and close it. In order to run it, we need to enter the following command:
$ python hello.py
It runs and prints HELLO.
Usually, when you install Python on Windows, it fails to correct the path, so you have to execute the following commands to create a symbolic link; otherwise, Python will not start correctly from the command line:
- cd c: \Windows
- mklink /H python.exe
- c: \python27\python.exe
In the next section, we will look at the Caesar cipher and ROT13 obfuscation techniques.