Python is an easy-to-learn programming language that's has a philosophy that includes, "readability counts," making Python a good choice as a first programming language.
Most programming languages encourage indentation as a best practice to help readers identify blocks of code. Python insists on four spaces of indentation after the first line of a block of code.
Python's a general purpose language that's platform independent. That is, you can run Python on Windows, Mac, Linux, or any other Operating System.
Python's a high-level language, abstracting away much of the detail so you can focus more on solving problems rather than on the details of the hardware and operating system.
Here are instructions for installing Python on Mac OSX, Windows, and on Linux. You'll likely want to learn Python on your local computer; then you'll probably want to start using Python to help you manage and automate your VPS hosting.
Install Python on Mac OSX
Mac OSX has Python 2.x installed by default as it's used to perform specific administrative tasks on your Mac. For this tutorial and in general, you'll likely want to install 3.x the most recent version of Python.
Install the HomeBrew package manager
If you haven't installed it yet, Home Brew is an excellent package manager for Mac OSX.
Open a terminal on your Mac then run this command to download and install HomeBrew.
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then check the version of HomeBrew using this command.
$ brew --version
Next, use HomeBrew to install Python 3 on your Mac.
$ brew install python3
Set up a Virtual Environment on your Mac
You'll likely want to set up a virtual environment in which to develop Python projects. A virtual environment separates a space within which you can create Python programs without affecting the rest of your computer.
$ mkdir Environments
$ cd Environments $ python3.6 -m venv dev_env $ source dev_env1/bin/activate
You're now ready to start coding in Python.
Write a Hello World script
Start with a simple Hello World script. Using your favorite text editor, create a file called hello.py. Enter the following into hello.py and save it.
From the command line run hello.py.
$ python hello.py
The script should return this.
Congratulations! You've written a Python script. You can build on this to learn Python and start creating useful scripts.
Install Python on Windows
For Windows, go to the Python.org download page then download and install Python for "all users."
Next, go to the Start menu and type cmd in the search box. Run cmd.exe as Administrator.
Then, change directory to the directory in which you installed Python and run this command to set the system path to Python:
setx PATH "%cd%;%path%;" pause
Set up a virtual environment on Windows
Open up Powershell, which is the command line environment within Windows that you'll use to create and run Python programs.
Set the execution policy scope to the current user.
$ Set-ExecutionPolicy -Scope CurrentUser
Powershell will then prompt you for the execution policy. Type the following:
Windows will prompt you asking if you want to change the execution policy. Anwer "y" for yes.
Install the Chocolatey package manager
$ $script = New-Object Net.WebClient
$ $script | Get-Member
Use the output to implement the method.
Install Chocolatey by running this command.
$ iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
To upgrade Chocatey (a command you may need in the future:
$ iwr https://chocolatey.org/install.ps1 -UseBasicParsing |
Install Python 3
$ choco install -y python3
Now, check what version you have installed. It should be 3.x (a version 3 variation such as Python 3.5.2)
$ python -V
Set up a virtual environment
$ mkdir Environments $ cd Environments
Then run this command to create a virtual environment. For this example, the virtual environment's called dev_env1, but you can name it anything you want.
$ python -m venv dev_env1
Your command prompt should look similar to this.
(dev-env1) PS c:\Users\You>
Install a simple editor and create your first Python script
Install nano or use your favorite editor.
$ choco install -y nano
Open nano (or your favorite editor) and type the following:
(dev-env1) PS c:\Users\You> python hello.py
The hello.py program should return just what you'd expect.
Congratulations, you've written your first Python program.
Install Python on Ubuntu
As always, first, make sure Ubuntu's package manager's up-to-date.
$ sudo apt-get update $ sudo apt-get upgrade $ sudo apt-get clean all
Then check to see if Python's already installed and which version is installed.
$ python3 -V
You may find that Python3's already installed.
Next, install the Python, package manager.
$ sudo apt-get install -y python3-pip
With pip, you can install Python packages with this command.
$ pip install package_name
Start by installing some development packages that you're likely to find useful.
$ sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
Setting up a Virtual Environment
To isolate your Python scripts from the rest of your VPS, create a virtual environment. This will enable you to code Python without worrying about making a mistake and doing any damage to any other packages installed on your VPS.
First, install the Python3 Virtual Environment: python3-venv.
$ sudo apt-get install -y python3-venv
Next, create a virtual environment in which to learn and practice Python.
$ mkdir environments $ cd environments $ python3 -m venv dev_env1
Before you can use your new dev environment, you'll need to activate it.
$ source test_env/bin/activate
Now, you can start coding Python!
Write Hello World!
You can install an Integrated Development Environment (IDE) to create and run Python programs, but for this introduction to Python, you can use any text editor to write Python scripts. For the rest of these lessons, it's assumed you're using your VPS. You can follow along as easily on your Mac or Windows machine.
With your favorite text editor, create a file called hello.py.
On your VPS using vi or nano, you could create a file call hello.py. Enter the following in the file.
Save the file. Now enter the following to run your script.
$ python hello.py
Congratulations, you've written and run the traditional "Hello World" as your first Python program. You can build on this first script to learn Python.
How to install and get started with Python on CentOS 7
>Update your system
$ sudo yum -y update $sudo yum -y upgrade $ sudo yum clean all
Install the Yum Utilities Package and CentOS Development Tools
$ yum install yum-utils -y
Then install the CentOS development tools.
$ yum install groupinstall development
$ Install IUS
IUS (Inline Upstream Stable) will ensure that you install the latest stable version of Python for CentOS.
$ Install Python 3
$ sudo yum -y install python36u
Install the pip Python package manager and python36u-devel
$ sudo yum -y install python36u-pip
$ sudo yum -y install python36u-devel
Create a virtual environment
To keep your development environment separate from the rest of your VPS, create a development environment.
$ mkdir environments $ cd environments
Next, run this command to start a virtual environment.
$ python3.6 -m venv dev_env1
Next, activate your virtual environment.
$ source dev_env1/bin/activate
Create Hello World!
Using your favorite text editor, create a file called hello.py with the following line in it.
It should return the following.
Congratulations, you've just created your first Python script on your virtual server. You're now ready to build on this to develop useful Python scripts.
You've now installed Python on your local computer (likely Windows or Mac), and you've installed Python on your VPS.
A good next step with Python would be to create some scripts to automate some common tasks that you might perform manually. Python's an excellent tool for automating routine tasks associated with VPS hosting.