Hostwinds Tutorials

Search results for:


Table of Contents


Installing Python
Using Python commands
Running Python scripts

How to Install Python on Linux

Tags: VPS,  Linux 

Installing Python
Using Python commands
Running Python scripts

If you have Linux Cloud SSD with Hostwinds and want to use Python, this is the guide for you. To get started, connect to your Hostwinds Cloud SSD via SSH. If you're not sure of how to connect over SSH, Hostwinds offers you a guide to connecting your VPS with SSH.

You'll also want to consider using a sudo user in either CentOS7 or Unbuntu to follow along in this guide:

Installing Python

The exact installation steps vary, but the easiest way to install Python to a Linux Cloud SSD is to use a particular piece of software called a package manager. To clarify, package managers automatically download and set up the software on your system for you.

In Ubuntu or Debian, the package manager called "aptitude" uses apt on the command line. To install Python on Ubuntu, just run this command as root:

sudo apt install python3

In CentOS, Red Hat and Fedora have a package manager called "RPM," better known as "Red Hat package manager." Additionally, something called "Yellowdog Updater," or yum, servers as the command-line frontend. To install Python, as root, use the command:

sudo yum install python3

Once these commands complete, Python will be installed in your system.

Using Python commands

Once Python has been installed on your system, you can try it out on the command line. Enter the command python3, and you can access the Python interpreter. This interpreter will let you run commands in Python and see the results in real-time.

Here's a glimpse of Python on the command line in CentOS 7 using the PuTTY SSH client:

Once you have the >>> prompt on the right-hand side, you can enter Python code. As an example, here is a small one-line program you can quickly run:

print("Hello World!")

Here is how it looks once run on the interpreter:

To exit out of the python interpreter and back into your shell prompt, type exit() to return to the shell prompt.

Running Python scripts

Running commands from the interpreter is good if you'd like to get the feel of Python. However, if you want to run more complete scripts, you'll most likely want to write it into an individual file called a script.

Python scripts mostly end in the .py extension, so you can make one by running a text editor called Nano, which will create a new file.

Run the command nano hello.py. You'll notice it'll pull up a new screen here, and it should look something like this:

Once in there, you can start typing Python code. The below code snippet asks for your name, then tells you hello:

#hello.py

print("Hello, may I know your name?")
greeting = input("Tell me your name here! > ")

if greeting:
        print("Hello, " + greeting + "!")
else:
        print("Hello!")

Here's how that looks in PuTTY:

Now, you'll need to save this file. In Nano, you can do this by holding down the control key and typing "X" on the keyboard. Then, Nano will prompt you to save the file, type "y" for yes, and then enter. You should now be back in the main shell.

Once the file saves, you can run it using python3 hello.py on the main shell page. Here is what running the program should look like:

python3 hello.py

Suppose you've made it this far. Congratulations! You've taken your first steps in using Python!

Written by Hostwinds Team  /  November 8, 2019