Python Install
## Python2.x Python Environment Setup
Python is a cross-platform programming language that can run on multiple operating systems.
In this chapter, we will introduce you to how to set up a Python development environment locally.
Python can be applied to multiple platforms, including Windows, Linux, and Mac OS X.
You can check whether Python is already installed on your local machine and the installed version by entering the python command in the terminal window.
* Windows: Includes Windows7 and above.
* macOS: Applicable 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 like Raspberry Pi, etc.
* Mobile platforms: Supports iOS and Android through specific frameworks (such as Kivy).
* Cloud platforms: Supports cloud service environments such as AWS, Google Cloud, Azure, etc.
* * *
You can view the latest Python source code, binary documentation, news, etc. on Python's official website:
Python Official Website: [https://www.python.org/](https://www.python.org/)
You can download Python's documentation from the following link. You can download documentation in HTML, PDF, and PostScript formats.
Python Documentation Download Address: [https://www.python.org/doc/](https://www.python.org/doc/)
* * *
## Python Installation
Python has been ported to many platforms (modified to work on different platforms).
You need to download the binary code applicable to your platform and then install Python.
If the binary code for your platform is not available, you need to manually compile the source code using a C compiler.
The compiled source code offers more functional options and provides more flexibility for Python installation.
The following are the download addresses for installation packages on various platforms:
!(#)
The following are the methods for installing Python on different platforms:
### Unix & Linux Platform Installation:
The following are the simple steps for installing Python on Unix & Linux platforms:
* Open a web browser and visit [https://www.python.org/downloads/source/](https://www.python.org/downloads/source/)
* Select the source code package suitable for Unix/Linux.
* Download and extract the compressed package.
* If you need to customize some options, modify _Modules/Setup_
* **Execute** the ./configure script
* make
* make install
After executing the above operations, Python will be installed in the /usr/local/bin directory, and the Python library will be installed in /usr/local/lib/pythonXX, where XX is the version number of Python you are using.
### Windows Platform Installation:
The following are the simple steps for installing Python on Windows platform:
* Open a web browser and visit [https://www.python.org/downloads/windows/](https://www.python.org/downloads/windows/)
!(#)
* Select the Windows platform installer 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. Just save the installation file to your local computer and run it to see if your machine supports MSI. Windows XP and higher already have MSI, and many older machines can also install MSI.
!(#)
* After downloading, double-click the download package to enter the Python installation wizard. The installation is very simple. You just need to use the default settings and keep clicking "Next" until the installation is complete.
### Mac Platform Installation:
Mac systems generally come with Python2.x environment pre-installed. You can also download the latest version from the link [https://www.python.org/downloads/mac-osx/](https://www.python.org/downloads/mac-osx/).
* * *
## Environment Variable Configuration
Programs and executable files can be in many directories, and these paths may not be in the executable search path provided by the operating system.
The 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.
The path variable in Unix or Windows is PATH (case-sensitive in Unix, case-insensitive in Windows).
In Mac OS, the installer changes the Python installation path during installation. If you need to reference Python in other directories, you must add the Python directory to the path.
### Setting Environment Variables on Unix/Linux
* **In csh shell:** Enter
setenv PATH "$PATH:/usr/local/bin/python" , and press Enter.
* **In bash shell (Linux):** Enter
export PATH="$PATH:/usr/local/bin/python" , and press Enter.
* **In sh or ksh shell:** Enter
PATH="$PATH:/usr/local/bin/python" , and press Enter.
**Note:** /usr/local/bin/python is the Python installation directory.
### Setting Environment Variables on Windows
Add the Python directory to the environment variable:
**In the command prompt (cmd):** Enter
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 "This PC" (or "Computer"), then click "Properties"
* Then click "Advanced System Settings"
* Select "Path" under the "System Variables" window, and double-click it!
* Then, in the "Path" line, add the Python installation path (mine is D:Python32). So add this path after the existing path. **ps: Remember, separate the paths directly with a semicolon ";"!**
* After the settings are successful, in the cmd command line, enter the command "python", and you will see the relevant output.
!(#)
* * *
## Python Environment Variables
The following are several important environment variables that apply to Python:
| Variable Name | Description |
| --- | --- |
| PYTHONPATH | PYTHONPATH is Python's search path. By default, all modules we import will be searched from PYTHONPATH. |
| PYTHONSTARTUP | After Python starts, it first looks for the PYTHONSTARTUP environment variable, then executes the code in the file specified by this variable. |
| PYTHONCASEOK | If the PYTHONCASEOK environment variable is set, Python will not distinguish between uppercase and lowercase when importing modules. |
| PYTHONHOME | Another 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 the command-line window and start writing Python code in the interactive interpreter.
You can do Python coding work 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 | Show debug information during parsing |
| -O | Generate optimized code ( .pyo file ) |
| -S | Do not introduce the location for searching Python paths at startup |
| -V | Output Python version number |
| -X | Exceptions based on built-ins (only for strings) after version 1.6 are deprecated. |
| -c cmd | Execute Python script and run the result as a cmd string. |
| file | Execute Python script in the given python file. |
### 2. Command-line Script
You can execute Python scripts on the command line by introducing the interpreter in your application, as follows:
$ python script.py # Unix/Linux
or
C:>python script.py # Windows/DOS
**Note:** When executing the script, please check if the script has executable permissions.
### 3. Integrated Development Environment (IDE: Integrated Development Environment): PyCharm
PyCharm is a Python IDE created by JetBrains, supporting macOS, Windows, and Linux systems.
PyCharm Features: Debugging, syntax highlighting, Project management, code navigation, intelligent prompts, auto-completion, unit testing, version control...
PyCharm Download Address: [https://www.jetbrains.com/pycharm/download/](https://www.jetbrains.com/pycharm/download/)
PyCharm Installation Address: [
!(#)
To install the PyCharm Chinese plugin, open the menu bar File, select Settings, then go to Plugins, click Marketplace, search for "chinese", and click Install to install:
!(#)
In the following learning, please make sure your environment has been set up successfully.
The examples given in the following chapters have been tested on Python 2.7.6 version.
YouTip