Python 3 Installation and Running Guide

From Excel batch processing for automated office work, to dashboard generation for data visualization, to lightweight fine-tuning scripts for large AI models - Python is the tool chain language of choice for almost every technology beginner and development veteran. Today we will start with the most basic environment-setup and code running methods to clear the first stumbling block for getting started with Python.

Install Python 3

Currently, Python still has two major version branches, 2.x (official maintenance has stopped) and 3.x (currently mainstream). This article is explained based on the latest stable version of Python 3.x. Please be sure to choose the 3.x branch for installation.

Windows system installation

Method 1: Official pure installation package (suitable for lightweight entry)

  1. Visit Python 官方下载中心
  2. The system will automatically identify your Windows bit number, just download the recommended Windows installer (64-bit) (please choose the corresponding version for 32-bit old devices)
  3. Key steps ⚠️: Open the first page of the installation program. **You must check "Add Python X.
  4. Click "Install Now" to complete it with one click (the default configuration is friendly enough for novices)

Anaconda is a one-stop environment package that includes Python, commonly used data science libraries (such as Pandas, NumPy) and development tools, and is universal across platforms:

  1. Visit Anaconda 官网下载页
  2. Select the free Individual Edition of the corresponding platform and download and install it.

macOS system installation

Method 1: Official pure installation package

  1. Also download the macOS version from Python 官方下载中心.pkgBag
  2. Double-click to run and follow the wizard's default "Continue" to complete the installation.

Homebrew is the most commonly used command line software management tool for macOS. If you haven’t installed it yet, you can run it in the terminal first:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After installing Homebrew, get the latest stable version of Python 3 with one click:

brew install python

Linux system installation

Most modern Linux distributions (such as Ubuntu 20.04+, Fedora 36+) come with Python 3 pre-installed by default, but the version may be older or missing some necessary components. If you need to upgrade/reinstall, you can choose the corresponding command according to the release version:

Debian / Ubuntu Series

# 更新软件源后安装
sudo apt update && sudo apt install python3 python3-pip

Fedora / RHEL / CentOS Stream Series

sudo dnf install python3 python3-pip

Arch Linux / Manjaro Series

sudo pacman -S python python-pip

Verify whether the installation is successful

After the installation is complete, we need to confirm Python and package management tools in Terminal/Command Linepipcan be called normally.

View Python version

  • Windows (used by default after clean installation)python):
    python --version
  • macOS / Linux
    python3 --version

If it shows something likePython 3.12.1The specific version number indicates that the installation is successful!

Check pip version

  • Windows
    pip --version
  • macOS / Linux
    pip3 --version

Tip: IfpipNot recognized, you can run it firstpython -m pip --versionSee if it can be called normally.


Two mainstream ways to run Python

Python provides two methods: interactive real-time testing and script file batch running, which are suitable for different scenarios.

1. Interactive REPL environment (suitable for learning and testing small codes)

The full name of REPL is "Read-Eval-Print Loop", which means read-execution-output loop. You enter a line of code and it will give the result immediately.

Enter REPL

  • Windows
    python
  • macOS / Linux
    python3

After successfully logging in, you will see the terminal appear>>>prompt (this is Python's interactive flag), you can now write code directly for testing:

>>> print("Hello, Python 3!")
Hello, Python 3!
>>> 1 + 2 * 3
7
>>> name = "技术小白"
>>> print(f"欢迎,{name}!")
欢迎,技术小白!

Exit REPL

There are two methods:

  1. Enter built-in functionsexit()orquit()and press Enter
  2. Shortcut key operations:
    • Linux / macOS:Ctrl + D
    • Windows:Ctrl + Zand then pressEnter

2. Batch running of script files (suitable for actual projects and formal codes)

You should use it when the code exceeds 3-5 lines or needs to be saved and executed repeatedly..pysuffixed script file.

Step 1: Create a script file

Open any plain text editor (VS Code, Notepad, TextEdit are all acceptable, Be careful not to use rich text editors such as Word/WPS), enter the following code and save it ashello.py

# hello.py 是我们的第一个脚本文件
# 以 # 开头的是注释,不会被 Python 执行
print("你好,来自脚本文件的 Python 代码!")
print("这是第二行输出~")

Step 2: Execute the script file

Open the terminal/command line and switch to the folder where the script file is located (for example, if you puthello.pyplaced on the desktop), then run:

  • Windows
    # 假设桌面路径是 C:\Users\你的用户名\Desktop
    cd C:\Users\你的用户名\Desktop
    python hello.py
  • macOS / Linux
    # 假设桌面路径是 ~/Desktop
    cd ~/Desktop
    python3 hello.py

After execution you will see two neat lines of output!


Quick FAQ for newbies

Question 1: Prompt "python / python3 is not an internal or external command"

This is because the system cannot find the installation path for Python and can be solved by platform:

  • Windows Clean Installation: Re-open the official installation package, select "Modify" to modify, check "Add Python X.
  • macOS/Linux: First confirm whether the installation is successful (try the installation command corresponding to the system again). If it still does not work, try to use the complete installation path call (for example, the default path for macOS Homebrew installation is/usr/local/bin/python3

Question 2: There are multiple Python versions on the computer, how to switch?

  • Windows: The official installation package will automatically include onepyLauncher, you can switch by specifying the version number:
    py -3.12  # 调用 3.12 版本
    py -3.9   # 调用 3.9 版本
  • macOS/Linux: It is recommended to use the pyenv tool for professional multi-version management. For installation and use, please refer to pyenv 官方文档

Must-have development tools and techniques for getting started

  • The first choice for lightweight entry: VS Code + Python extension (search "Python" in the plug-in store and install the official Microsoft one), supports syntax highlighting, auto-completion, and one-click operation
  • The first choice for full-stack Python development: PyCharm Community Edition (free version), which comes with powerful project management, debugging, and code refactoring functions

2. Virtual environment: an artifact to isolate project dependencies

If you are working on multiple Python projects at the same time, each project may require different third-party library versions (for example, project A requires Pandas 1.5, and project B requires Pandas 2.0). At this time, you need to use virtual environment to isolate the dependencies of each project to avoid conflicts.

Create and activate virtual environment

  1. Open Terminal in the project folder
  2. Create a virtual environment (myenvIt is the name of the virtual environment and can be changed at will):
    python3 -m venv myenv  # macOS / Linux
    python -m venv myenv   # Windows
  3. Activate virtual environment:
    • Linux / macOS
      source myenv/bin/activate
    • Windows(CMD)
      myenv\Scripts\activate.bat
    • Windows(PowerShell)
      myenv\Scripts\Activate.ps1

After activation, the terminal will appear at the beginning(myenv)sign, indicating that you are now in the virtual environment! 4. Exit the virtual environment:

deactivate

Summarize

Today we have completed the full process of getting started with Python 3:

  1. Install the latest stable version of Python 3, Be sure to remember to check "Add to PATH"
  2. Learned to verify installation in two ways
  3. Mastered the two core operating methods of "interactive REPL testing small code" and "script file running formal code"
  4. Understand the solutions to common problems faced by novices and the necessary virtual environment for getting started.

Now, you are ready to start your Python programming journey!