Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Environment Setup

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.


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:

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:

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

**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:


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.