Python Environment Setup
Python is a cross-platform programming language that can run on many operating systems.
In this chapter, we will introduce how to set up a Python development environment locally.
Python can be used on multiple platforms, including Windows, Linux, and Mac OS X.
You can type the python command in a terminal window to check whether Python is installed locally and which version is installed.
- Windows: including Windows 7 and later versions.
- macOS: applies to all major macOS versions.
- Linux: almost all major Linux distributions, such as Ubuntu, Debian, Fedora, CentOS, etc.
- Unix: supports Unix-like systems such as FreeBSD, OpenBSD, etc.
- Embedded systems: devices such as Raspberry Pi.
- Mobile platforms: supports iOS and Android through specific frameworks (such as Kivy).
- Cloud platforms: supports cloud service environments such as AWS, Google Cloud, and Azure.
Python Download
The latest Python source code, binaries, documentation, news, and more can be found on the official Python website:
Python official website:https://www.python.org/
You can download Python documentation from the following links. Documentation is available in formats such as HTML, PDF, and PostScript.
Python documentation download addresses:https://www.python.org/doc/
Python Installation
Python has been ported to many platforms (modified so that it works on different platforms).
You need to download the binary code for your platform and then install Python.
If binary code for your platform is not available, you need to compile the source code manually using a C compiler.
Compiling from source gives you more choices in features and provides greater flexibility for the Python installation.
The following are download addresses for installation packages on each platform:

The following are methods for installing Python on different platforms:
Installing Python on Unix & Linux platforms:
The following are the simple steps for installing Python on Unix & Linux platforms:
- Open a WEB browser and visithttps://www.python.org/downloads/source/
- Select a source archive suitable for Unix/Linux.
- Download and extract the archive.
- If you need to customize some options, modifyModules/Setup
- Executing ./configure script
- make
- make install
After performing the above steps, Python will be installed in the /usr/local/bin directory, and the Python libraries will be installed in /usr/local/lib/pythonXX, where XX is the version number of the Python you are using.
Installing Python on the Window platform:
The following are simple steps for installing Python on the Windows platform:
-
Open a WEB browser and visithttps://www.python.org/downloads/windows/

- Select the Windows platform installation package from the download list. The package format is:python-XYZ.msi file, where XYZ is the version number you want to install.
-
To use the installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Simply save the installation file to your local computer and run it to see if your machine supports MSI. Windows XP and later already have MSI, and many older machines can install MSI as well.

-
After downloading, double-click the downloaded package to enter the Python installation wizard. Installation is very simple: just keep the default settings and click 'Next' until the installation is complete.
Installing Python on the MAC platform:
MAC systems generally come with a Python 2.x environment pre-installed. You can also visit the link https://www.python.org/downloads/mac-osx/ to download and install the latest version.
Environment variable configuration
Programs and executable files can be located in many directories, and these paths are often not in the search path that the operating system uses to find executables.
path is stored in environment variables, which are named strings maintained by the operating system. These variables contain information about available command-line interpreters and other programs.
In Unix or Windows, the path variable is PATH (UNIX is case-sensitive, Windows is not).
In Mac OS, the installation process changes the Python installation path. If you need to reference Python from another directory, you must add the Python directory to your path.
Setting environment variables in Unix/Linux
- In the csh shell: Input
setenv PATH "$PATH:/usr/local/bin/python"
, then press Enter. - In the bash shell (Linux): Input
export PATH="$PATH:/usr/local/bin/python"
, then press Enter. - In the sh or ksh shell: Input
PATH="$PATH:/usr/local/bin/python"
, then press Enter.
**Note: **/usr/local/bin/python is the Python installation directory.
Setting environment variables in Windows
Add the Python directory to environment variables:
In the command prompt (cmd): Input
path=%path%;C:\Python
Press Enter.
**Note: **C:\Python is the Python installation directory.
You can also set it in the following way:
- Right-click 'Computer', then click 'Properties'
- Then click 'Advanced system settings'
- In the 'System variables' window, select 'Path' and double-click it!
- Then, in the 'Path' line, add the Python installation path (mine is D:\Python32), so add that path at the end. ps: Remember to separate paths directly with a semicolon ';'!
- Finally, after the setup is successful, type the command 'python' in the cmd command line, and the relevant information will be displayed.

Python Environment Variables
The following important environment variables apply to Python:
| Variable Name | Description |
|---|---|
| PYTHONPATH | PYTHONPATH is the Python search path. By default, the modules we import are searched for in PYTHONPATH. |
| PYTHONSTARTUP | After Python starts, it first looks for the PYTHONSTARTUP environment variable, and then executes the code in the file specified by this variable. |
| PYTHONCASEOK | Adding the PYTHONCASEOK environment variable makes Python case-insensitive when importing modules. |
| PYTHONHOME | Another type of module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directory, making it easier to switch between two module libraries. |
Running Python
There are three ways to run Python:
1. Interactive interpreter:
You can enter Python through a command-line window and start writing Python code in the interactive interpreter.
You can write Python code on Unix, DOS or any other system that provides a command line or shell.
$ python # Unix/Linux
or
C:>python # Windows/DOS
The following are Python command-line parameters:
| Option | Description |
|---|---|
| -d | Display debugging information during parsing |
| -O | Generate optimized code (.pyo files) |
| -S | Do not include the location used to find the Python path at startup |
| -V | Output the Python version number |
| -X | Since version 1.6, built-in exceptions (strings only) have been deprecated. |
| -c cmd | Execute a Python script and use the result as a cmd string. |
| file | Execute a Python script in the given Python file. |
2. Command-line script
By bringing the interpreter into your application you can execute Python scripts from the command line, as shown below:
$ python script.py # Unix/Linux
or
C:>python script.py # Windows/DOS
**Note:** When running a script, check whether it has execute permission.
3. Integrated Development Environment (IDE): PyCharm
PyCharm is a Python IDE built by JetBrains, supporting macOS, Windows and Linux.
PyCharm features: debugging, syntax highlighting, project management, code navigation, smart hints, auto-completion, unit testing, version control...
PyCharm download address: https://www.jetbrains.com/pycharm/download/
PyCharm installation address:https://www.runoob.com/pycharm/pycharm-install.html

To install the PyCharm Chinese plugin, open the File menu, select Settings, then Plugins, click Marketplace, search for chinese, and then click install:

In the following lessons, please make sure your environment has been set up successfully.
The examples given in the following chapters have been tested and pass under Python 2.7.6.