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

Python3 Environment Setup

Python3 Environment Setup

Python3 has excellent cross-platform compatibility and runs stably on the three major operating systems Windows, Linux and Mac OS X, while also supporting many other platforms and environments, including:

In addition, Python can be ported to run in Java and .NET virtual machine environments.


Python3 Download

The latest Python3 source code, binary documentation, news and more can be found on the official Python website.

Python official website:https://www.python.org/

Python3 provides complete Chinese documentation:https://docs.python.org/zh-cn/3/


Python Installation

Python has excellent cross-platform compatibility, has been ported and adapted to multiple operating systems, and runs stably on mainstream platforms such as Windows, macOS and Linux.

We can preferably download the binary installer for the corresponding platform and complete installation quickly through a graphical wizard or the command line, which is the most convenient and efficient installation method.

If no binary installer is available for the operating system, you can obtain the Python source code and compile and install it manually with a C compiler.

Compared with installing a binary package, compiling the source offers richer feature customization options, making Python environment configuration more flexible.

The following are the download addresses of Python installers for each platform:

Source Code Can be used for installation on Linux.

The following are methods for installing Python3 on different platforms.

Installing Python on the Window platform:

The following are the simple steps for installing Python on the Window platform.

Open a WEB browser and visit https://www.python.org/downloads/windows/ :

These links provide different types of Python installers for different Windows systems and usage scenarios:

Remember to check Add Python 3.6 to PATH。

Note: if you do not check Add Python3.6 to PATH", the command line will not recognize the python/python3 commands, and you need to configure the environment variables manually.

Press Win+R, type cmd to bring up the command prompt, and enter python:

You can also search in the Start menu for IDLE:

Installing Python3 on Unix & Linux platforms

Most Linux distributions come with Python3; if it is not installed or needs upgrading, it can be installed through the package manager.

Installing from source

The following are the simple steps for installing Python on Unix & Linux platforms:

Take the ** Python3.6.1** version as an example:


# tar -zxvf Python-3.6.1.tgz
# cd Python-3.6.1
# ./configure
# make && make install

Ubuntu/Debian

Open a terminal and run the following commands:

# 更新软件源
sudo apt update
# Install Python3 and pip3 (Python package manager)
sudo apt install python3 python3-pip -y

CentOS/RHEL

Open a terminal and run the following commands:


# CentOS 7
sudo yum install epel-release -y
sudo yum install python3 python3-pip -y

# CentOS 8/RHEL 8
sudo dnf install python3 python3-pip -y

Check whether Python3 works properly:

# python3 -V
Python 3.6.1

Installing Python on the MAC platform:

MAC systems come with a Python environment; you can download the latest version from the link https://www.python.org/downloads/mac-osx/ to download and install the latest version.

You can also refer to the source installation method to install it.

Verify that the Python3 environment is installed successfully

To verify the Python3 version, open the command prompt (Windows) or terminal (macOS/Linux) and run the following command:

# 通用命令(推荐,所有系统兼容)
python3 --version
# Supplement: If PATH is configured on Windows, it can also be executed
python --version

If the output is similar to Python 3.11.4, Python3 has been installed successfully.

Verify pip3 (the Python package management tool): pip3 is the default package management tool of Python3, used to install third-party libraries; the verification command is:

# 通用命令
pip3 --version
# Windows supplementary commands
pip --version

If the output is similar to pip 23.1.2 from xxx (python 3.11), pip3 is available.


Environment variable configuration

If the python command above runs successfully, the environment is configured and no extra configuration is needed; this section can be skipped.

The directories holding executable programs are often not in the system default search path, and the system's PATH environment variable (case-sensitive on Unix, not on Windows) is exactly what stores the search paths for executables.

In Mac OS, to reference Python from a non-default directory you need to manually add the Python installation directory to PATH.

Setting environment variables in Unix/Linux

Note: /usr/local/bin/python is the Python installation directory; replace it with your actual path.

bash shell(Linux):


export PATH="$PATH:/usr/local/bin/python"

csh shell:


setenv PATH "$PATH:/usr/local/bin/python"

sh/ksh shell:


PATH="$PATH:/usr/local/bin/python"

Setting environment variables in Windows

If Add Python.exe to PATH was not checked when installing Python3, the command line will not recognize the python/python3 commands, so the environment variables must be configured manually:

For more detailed configuration, see:https://www.runoob.com/w3cnote/add-python-to-path-on-windows-11.html

Close the original command prompt, reopen it and run the verification command for the changes to take effect.

The following describes several environment variables applied to Python:

Environment variable name Core Role
PATH The search path where the system looks for the Python interpreter and executables
PYTHONPATH The search path where Python looks for third-party libraries and custom modules
PYTHONHOME Specifies the Python installation root directory, telling the interpreter where the core/standard libraries are stored
PYTHONSTARTUP Specifies the path of a script file automatically executed when the Python interactive interpreter starts
PYTHONCASEOK Windows only; makes Python ignore case when importing modules
PYTHONDONTWRITEBYTECODE Prevents Python from generating .pyc / .pyo bytecode cache files at runtime

Running Python

There are three ways to run Python:

1. Interactive interpreter:

You can enter Python from 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

The following are Python command-line parameters:

Option Description
-d Enable debug mode, showing detailed debugging information during code parsing and interpreter execution
-O Generate optimized code; produce .pyo optimized bytecode files when compiling scripts (ignoring assertions and other debugging-related code)
-OO Deeply optimize the code, producing .pyo files and removing all docstrings from the code to further reduce file size
-S Do not automatically import the site module at startup, i.e. do not load the configuration related to finding Python module paths (such as the site-packages directory)
-V / --version Print the currently installed Python version number and exit the interpreter immediately
-vv Print detailed version information (including the build environment, dependent libraries and other extra information)
-X Since Python 1.6, the use of built-in exceptions for strings has been deprecated; this parameter provides compatibility with those older features
-h / --help Show the complete help for all Python command-line parameters and exit the interpreter immediately
-c cmd Execute a specified Python code snippet directly on the command line (cmd is code in string form), without writing a .py script file
-m module Run a specified Python module as a module (such as pip or http.server), automatically finding the module path and executing it
-i After running the specified Python script, automatically enter the interactive interpreter environment for subsequent debugging and additional code execution
-b Issue a warning when an incompatible comparison between bytes and str is encountered
-bb Raise an error immediately and terminate the program when an incompatible comparison between bytes and str is encountered
-u Disable buffering of standard output (stdout) and standard error (stderr) so that logs or output are printed in real time
file Specify the path of the Python script file to execute (absolute or relative); the interpreter loads and runs the code in that file
-q When entering the interactive interpreter, hide the welcome message and show the prompt directly

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

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

PyCharm interface:


** More essential tools

** Anaconda integrated environment

AFor data science and machine learninga bundled Python distribution that includes:

** Compared with pip,conda is better suited to switching between multiple environments, and is more stable in data science scenarios.

** Installation and usage: Anaconda Tutorial

** uv Python package and environment management tool

uv is a high-speed Python toolchain developed by Astral and built on Rust.

** An all-in-one alternative to pip、virtualenv、pip-tools and similar tools

** Installation and usage: uv Tutorial

** Jupyter Notebook interactive computing tool

AA web-based interactive programming environment, suitable for learning, experimentation and data analysis.

** Notebook files are in JSON format and consist of multiple cells that can mix code and documentation.

** Installation and usage: Jupyter Notebook Tutorial