Python installation and environment configuration

Python 3 is available for Windows, Mac OS, and most Linux operating systems. Even though Python 2 is currently available for many other operating systems, there are some systems where Python 3 is not yet supported or has been supported but has been removed from the system, leaving only the older Python 2 version.

In this tutorial, we focus on how to install the latest version of Python 3 (current new version: Python 3.6.1) on Windows 10 and Ubuntu systems.

Install Python 3 on Windows 10

The binaries of the latest version of Python 3 (Python 3.5.1) can be downloaded from the download page of the official Python website: http://www.python.org/downloads/windows/, and the following different installation options are available –

Select here: Download Windows x86-64 executable installer Download. Once the download is complete, double-click the python-3.6.1-amd64.exe executable file.

Step 1: Double-click the python-3.6.1-amd64.exe executable file as shown below –

Step 2: Select “Cusomize installation” as shown below –

Step 3: Select “Next>“, here choose to install in D:\Program Files\Python36, as shown below –

Step 4: Start installing “Install“ as follows –

Step 5: After the installation is complete, select Close as shown below –

Test the installation results

Since we have selected “Add Python 3.6 to PATH” in the first step of installation, there is no need to set environment variables separately here. If this is not selected, you should need to add Python 3.6 to your environment variables.
Assuming you have followed the above steps to complete the installation, now open the command prompt and enter python and press Enter –

At this point, the installation of Python 3.6 on Windows 10 system has been completed.

Install Python 3 on Ubuntu

First, let’s take a look at what version of Python is installed on the Ubuntu system. Enter python on the terminal, as shown below –

zyiz@ubuntu:~$ python -version
The program 'python' can be found in the following packages:
 *python-minimal
 *python3
Try: sudo apt install <selected package>
zyiz@ubuntu:~$

In the results shown above, Python has not been installed.

First case:
If you are using Ubuntu 14.04 or 16.04, you can use J Fernyhough’s PPA: http://launchpad.net/~jonathonf/ + archive/ubuntu/python-3.6 to install Python 3.6:

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6

Second case:
If you are using Ubuntu 16.10 or 17.04, Python 3.6 is in the Universe repository, just upgrade apt-get and then install it –

sudo apt-get update
sudo apt-get install python3.6

Now, check out the current version of Ubuntu –

zyiz@ubuntu:~$ sudo lsb_release -a
[sudo] password for zyiz:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
zyiz@ubuntu:~$

Tip: Ubuntu cannot find the solution to the problem of add-apt-repository. Execute the installation command: apt-get install python-software-properties. In addition, you must install apt-get install software-properties-common, then you can use add-apt-repository.

According to the system information displayed above, the system version is: Ubuntu 16.04.1 LTS, so it belongs to the first case to install Python 3.6, so the complete installation steps are as follows –

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6

Note that the execution of the above command may be interrupted or errors occur. You can try executing it several times.

When the above command is executed successfully, it will also have Python 2.7 installed by default. Enter: python at the command line prompt, then it will use Python 2.7. If you want to use Python 3.6, then you can directly enter: python3.6, and the verification installation result is as follows –

Compile and install Python 3.6 from source
Alternatively, if you have the time and energy, you can try installing Python 3.6 by compiling from source. Source code download address: http://www.python.org/ftp/python/3.6.1/

First, you need to install some build dependencies using the following commands.

sudo apt install build-essential checkinstall
 
sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

Then, download the Python 3.6 source code from python.org.

wget http://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz

Next, unzip the tarball.

tar xvf Python-3.6.0.tar.xz

Now cd into the source directory, configure the build environment and install it.

cd Python-3.6.0/
 
./configure
 
sudo make altinstall

Makes the altinstall command skip creating symlinks, so /usr/bin/python still points to an older version of Python, ensuring that Ubuntu systems will not Interrupt.

Once this is complete, you can use Python 3.6 by typing the following command:

$ python3.6

Following is the list of all available command line options ?

Number Options Description
1 -d Provide debugging output
2 -O Generate optimized bytecode (the result is a .pyo file)
3 -S Do not run the import site to find the Python path when starting up
4 -v Verbose output (detailed tracking of import statements)
5 -X Disable built-in class-based exceptions (only use strings); already as of version 1.6 Obsolete
6 -c cmd Run Python script as cmdString sent
7 file Python script run from given File

Command line script

Python scripts can be executed from the command line by invoking the interpreter within an application, as shown in the following example.

$python script.py # Unix/Linux
 
or
 
python% script.py # Unix/Linux
 
or
 
C:>python script.py # Windows/DOS

NOTE – Make sure the file permission mode allows execution.

Integrated Development Environment

You can also run Python from a graphical user interface (GUI) environment if Python’s GUI applications are supported on your system.

Unix – IDLE is the first Unix IDE for Python.

Windows – PythonWin is the first Windows graphical user interface for Python, an IDE with a GUI.

Macintosh – Macintosh versions of Python and the IDLE IDE are available from the main website and can be downloaded as MacBinary or BinHex’d files.

If you are having trouble setting up your environment correctly, you may want to ask your system administrator for help. Make sure the Python environment is set up correctly to work properly.

Note – All examples given in subsequent chapters are executed using the Python 3.6.1 version available on Windows 7 and Ubuntu Linux.

Next chapters:

Python command line parameters

Python variable type

Python basic operators

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treePreparatory knowledgeInstalling Python353358 people are learning the system