Back to series
· 11 min read

Setting Up a Python Development Environment


1. What Is a Python Development Environment?

A Python development environment is the collection of software, tools, and configurations required to run and develop Python programs.

It includes not only the Python interpreter but also code editors, dependency management tools, virtual environments, debugging tools, and project configuration.

A complete Python development environment usually includes the following parts:

Component Main Purpose Common Tools
Python interpreter Parses and executes Python code CPython, PyPy
Code editor Write and edit code VS Code, PyCharm
Package management tool Install and manage third-party libraries pip, uv, Conda
Virtual environment Isolate dependencies for different projects venv, virtualenv
Python version management tool Manage multiple Python versions pyenv, uv
Debugging tool Locate and analyze program issues pdb, debugpy
Code quality tool Check code style and errors Ruff, mypy
Version control tool Manage code changes Git

1. Python Interpreter

The Python interpreter is the core component for running Python programs.

The .py files written by developers need to be processed by the Python interpreter before the code in them can be executed.

Common implementations include:

  • CPython: The official primary implementation of Python, developed in C, and the interpreter used by default in most projects.
  • PyPy: A Python implementation that uses JIT technology to optimize execution performance.
  • MicroPython: A lightweight implementation for microcontrollers and embedded devices.
  • Jython: A Python implementation that runs on the JVM, mainly targeting specific Java integration scenarios.

For most web development, automation, data analysis, and artificial intelligence projects, CPython is the preferred choice.

2. Code Editor

Code editors are used to write, view, and maintain Python source code.

Common choices:

  • Visual Studio Code: Lightweight and rich in plugins, suitable for a variety of development scenarios.
  • PyCharm: Focused on Python development and provides more complete project management and debugging capabilities.
  • JupyterLab: Suitable for data analysis, scientific computing, and interactive programming.
  • Vim and Neovim: Suitable for users who prefer terminal-based development.

The editor itself usually does not include a complete Python runtime environment and needs to be configured with an interpreter.

3. Package Management Tools

The Python standard library provides pip for installing and managing third-party Python packages.

For example:

python -m pip install requests

Modern Python projects can also use tools such as uv to manage dependencies, virtual environments, and Python versions.

4. Virtual Environments

Different projects may depend on different versions of third-party libraries.

For example:

  • Project A uses Django 5.x.
  • Project B uses Django 4.x.
  • Project C uses a specific version of NumPy.

If all projects share the same set of global dependencies, version conflicts are likely.

A virtual environment can create a relatively independent Python package installation space for each project.

2. Overall Architecture of a Python Development Environment

Before formal installation, you need to first understand the relationships between the components.

A typical Python project environment can be represented as:

flowchart TD
    A["Operating System"] --> B["Python Interpreter"]
    B --> C["Virtual Environment"]
    C --> D["Project Dependencies"]
    C --> E["Project Code"]
    F["Code Editor"] --> E
    F --> G["Debugging Tools"]
    G --> B
    H["Package Management Tools"] --> D
    I["Version Management Tools"] --> B

    style A fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style G fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155

    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

A few concepts need attention:

  • The Python interpreter is responsible for executing code.
  • Virtual environments isolate project dependencies.
  • pip or uv installs and manages packages.
  • The editor provides the development experience.
  • Debugging tools inspect program execution.
  • Version management tools select and manage Python versions.

These tools can be used together; they do not all need to come from the same software.

III. Installing the Python Interpreter

The Python interpreter is the foundation of the entire development environment.

It is recommended to get the installer from the official Python website:

https://www.python.org/downloads/

1. Choosing a Python Version

Before installing, you need to determine which version to use.

Main considerations when choosing:

  1. Whether it is within the official maintenance period.
  2. Whether third-party dependencies are compatible.
  3. Whether the operating system supports it.
  4. Whether the project depends on a specific Python version.
  5. Whether the team has a unified version requirement.

For new projects, it is recommended to prioritize evaluating a stable version that is still maintained.

If the project uses third-party frameworks such as PyTorch, TensorFlow, or NumPy, you also need to check the Python version compatibility range of the corresponding framework.

Do not ignore dependency library compatibility just because the Python version number is newer.

2. Installing Python on Windows

On Windows, you can install it using the official Python installer.

Main steps:

  1. Open the official Python download page.
  2. Select the Windows installer.
  3. Download the installation file suitable for the system architecture.
  4. Launch the installer.
  5. Select the installation path and features as needed.
  6. Complete the installation.

During Windows installation, pay special attention to PATH configuration.

If the installer provides the Add Python to PATH option, you can decide whether to check it based on your own environment management approach.

After configuring PATH, you can usually use the python command directly in the terminal.

If you do not want to modify the system PATH, you can also use Python Launcher:

py --version

View installed Python versions:

py -0p

Run a program with a specified version:

py -3.12 --version

If multiple Python versions are installed, Python Launcher can help select a specific version.

3. Installing Python on macOS

macOS usually comes with system-level Python-related components, but it is not recommended to use the system-provided Python as the project development environment.

You can install it via the official Python installer package, or use Homebrew:

brew install python

After installation, check the version:

python3 --version

Check the interpreter location:

which python3

On macOS, the common command is python3, not python.

If installing via Homebrew, note that different chip architectures and Homebrew installation directories may differ.

4. Installing Python on Linux

Linux distributions usually provide Python through the system package manager.

On Ubuntu or Debian systems, you can use:

sudo apt update
sudo apt install python3 python3-pip python3-venv

On Fedora systems, you can use:

sudo dnf install python3 python3-pip

On Arch Linux, you can use:

sudo pacman -S python python-pip

After installation, check:

python3 --version

Note:

Do not arbitrarily replace the Python that comes with the Linux system.

Some system tools depend on the system Python. If you directly modify the system interpreter or delete system dependencies, you may affect the package manager and other system components.

It is recommended to manage the project Python environment separately from the system Python.

5. Checking Whether Python Was Installed Successfully

After installation, open a terminal or command prompt.

Windows:

python --version

Or:

py --version

macOS and Linux:

python3 --version

If the output is similar to:

Python 3.x.x

This indicates that the interpreter can already be invoked using the corresponding command.

You can also view detailed Python information:

python -c "import sys; print(sys.version)"

On macOS and Linux, if you are using python3, run:

python3 -c "import sys; print(sys.version)"

IV. Configuring the PATH Environment Variable

PATH is the environment variable the operating system uses to locate executable programs.

When the user types the following in the terminal:

python --version

the operating system searches for the corresponding executable file in the order of the directories listed in PATH.

If the Python installation directory is not added to PATH, you may encounter a "command not found" error.

1. How PATH Works

You can think of PATH as a list of directories.

flowchart TD
    A["Enter the python command"] --> B["OS reads PATH"]
    B --> C["Search directories in order"]
    C --> D{"Executable found?"}
    D -->|Yes| E["Launch the Python interpreter"]
    D -->|No| F["Report command not found"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#FBE9E9,stroke:#D9AAAA,color:#334155

    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

2. Configuring PATH on Windows

If the installer did not configure PATH automatically, you can add the Python installation directory manually.

For example:

C:\Users\username\AppData\Local\Programs\Python\Python312\

You usually also need to add the Scripts directory:

C:\Users\username\AppData\Local\Programs\Python\Python312\Scripts\

Steps:

  1. Open Windows Settings.
  2. Search for "environment variables".
  3. Open the system environment variables configuration screen.
  4. Locate Path under User variables or System variables.
  5. Add the Python installation directory.
  6. Add Python's Scripts directory.
  7. Save the configuration.
  8. Reopen the terminal.

Check again:

python --version
pip --version

Note that user variables and system variables have different scopes.

User variables typically apply only to the current user, while system variables may affect all users.

3. Configuring PATH on macOS and Linux

macOS and Linux usually manage environment variables through shell configuration files.

Common configuration files include:

  • ~/.zshrc
  • ~/.bashrc
  • ~/.bash_profile

For example:

export PATH="/your/python/bin:$PATH"

After saving, reload the configuration:

source ~/.zshrc

If you use Bash:

source ~/.bashrc

Which file to modify depends on the shell you are currently using and how the terminal is started.

4. Checking Whether PATH Takes Effect

Windows:

where python
where pip

macOS and Linux:

which python3
which pip3

You can also use Python to check the interpreter's actual path:

python -c "import sys; print(sys.executable)"

If the Python found by the terminal is not the one you expect, you usually need to check the PATH order and whether the current environment is activated.

V. Choosing a Python Version Management Tool

When you need to run multiple Python projects on one computer, you may need to install several Python versions at the same time.

For example:

  • An old project needs Python 3.10.
  • A new project uses Python 3.14.
  • An experimental project needs to test Python 3.15.

If you frequently uninstall and reinstall Python, management becomes rather troublesome.

Version management tools can solve this problem.

1. pyenv

pyenv is a common Python version management tool.

It can install multiple Python versions and switch interpreters across different projects or shell environments.

Common commands:

pyenv install 3.12.10

View installed versions:

pyenv versions

Set the global version:

pyenv global 3.12.10

Set the project version for the current directory:

pyenv local 3.12.10

Check the current version in use:

pyenv version

Note:

  • Native pyenv primarily targets macOS and Linux.
  • On Windows, consider pyenv-win or another version management solution.
  • Compiling Python may require a compiler and related development dependencies from the system.
  • pyenv manages Python interpreter versions; it is not equivalent to a full project dependency management tool.

2. uv

uv is a Python package and project management tool developed in Rust.

It can manage:

  • Python versions.
  • Virtual environments.
  • Project dependencies.
  • Lock files.
  • Command-line tool environments.
  • Running and building projects.

After installation, you can use:

uv --version

Install a specified Python version:

uv python install 3.14

List available Python versions:

uv python list

Create a virtual environment:

uv venv --python 3.14

Install dependencies:

uv pip install requests

For developers who want to use a single tool for version management, project dependencies, and virtual environment management, uv is worth considering.

3. Conda

Conda is a tool for multi-language environments and package management, commonly used in data science, scientific computing, and artificial intelligence.

It can manage not only Python packages but also some non-Python binary dependencies.

Create an environment:

conda create -n myproject python=3.12

Activate the environment:

conda activate myproject

Install dependencies:

conda install numpy pandas

List environments:

conda env list

Deactivate the environment:

conda deactivate

Conda is particularly suitable for projects that need to manage complex scientific computing dependencies.

4. Comparison of Version Management Tools

Tool Core purpose Use cases
pyenv Python version management Multiple Python interpreters coexisting
uv Python project and environment management General Python project development
Conda Multi-language environment and package management Data science, scientific computing, AI
System package manager Install system software packages OS-level Python installation

You do not need to install all of these tools at the same time.

For general development projects, you can use the official Python installer with venv and pip.

Developers who want to simplify project management can consider uv.

For scientific computing and AI projects, you can consider Conda depending on the dependencies.

6. Installing and Configuring a Code Editor

After the Python interpreter is installed, you still need to choose a code editor.

1. Visual Studio Code

Visual Studio Code is a lightweight code editor that supports Python development capabilities through extensions.

Official website:

https://code.visualstudio.com/

After installation, it is recommended to set up the following extensions:

Extensions Main purpose
Python Provides Python language support
Pylance Provides code completion, type analysis, and navigation
Python Debugger Provides Python debugging capabilities
Jupyter Supports Notebook files
Ruff Provides code checking and formatting capabilities

Among them, the Python extension and Pylance are common foundational configurations.

2. Configure the Python Interpreter

Open the project folder in VS Code.

Press:

Ctrl + Shift + P

On macOS, typically use:

Command + Shift + P

Search for:

Python: Select Interpreter

Then select the Python interpreter used by the project.

If a virtual environment has already been created, you can select the .venv interpreter in the project directory.

For example:

Windows:

.venv\Scripts\python.exe

macOS and Linux:

.venv/bin/python

After making the selection, VS Code will use the corresponding interpreter to run and analyze the project code.

3. PyCharm

PyCharm is a Python integrated development environment provided by JetBrains.

Official website:

https://www.jetbrains.com/pycharm/

It provides relatively complete Python development features:

  • Project management.
  • Code completion.
  • Syntax analysis.
  • Breakpoint debugging.
  • Unit testing.
  • Virtual environment configuration.
  • Git integration.
  • Database tools.

PyCharm is suitable for developers who need full IDE capabilities.

4. JupyterLab

JupyterLab is suitable for interactive programming, data analysis, and machine learning experiments.

Installation:

python -m pip install jupyterlab

Start:

jupyter lab

Jupyter Notebook can place code, text, charts, and execution results in the same document.

It is very suitable for data exploration and algorithm experiments, but may not necessarily be suitable as the primary development method for all large software projects.

VII. Create a Python Virtual Environment

A virtual environment is a very important concept in Python project development.

It allows each project to have an independent space for installing third-party packages.

1. Why Are Virtual Environments Needed?

Suppose two projects depend on different versions of Flask:

Project A: Flask 2.x
Project B: Flask 3.x

If the two projects share the same set of global Python packages, version conflicts may occur.

Virtual environments can isolate dependencies within each project's environment.

flowchart TD
    A["System Python"] --> B["Project A virtual environment"]
    A --> C["Project B virtual environment"]
    B --> D["Project A dependencies"]
    C --> E["Project B dependencies"]
    D --> F["Flask 2.x"]
    E --> G["Flask 3.x"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style G fill:#FFF5DE,stroke:#E8D29A,color:#334155

    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Here, it is important to understand a key distinction:

A virtual environment usually does not copy a complete Python installation directory; instead, it provides an independent package installation space through environment configuration and interpreter references.

It mainly solves third-party dependency isolation and is not equivalent to a complete operating-system-level sandbox.

2. Create a Virtual Environment Using venv

venv is a virtual environment tool provided by the Python standard library.

Enter the project directory:

mkdir python-demo
cd python-demo

Create a virtual environment:

python -m venv .venv

If you're using macOS or Linux, use python3:

python3 -m venv .venv

After running this, a .venv folder will be created in the project directory.

A common directory structure:

python-demo/
├── .venv/
│   ├── bin/
│   ├── include/
│   ├── lib/
│   └── pyvenv.cfg
└──

On Windows, the virtual environment's executable files are usually located in:

.venv/
└── Scripts/
    ├── python.exe
    └── pip.exe

3. Activate the virtual environment

Windows PowerShell:

.venv\Scripts\Activate.ps1

Windows CMD:

.venv\Scripts\activate.bat

macOS and Linux:

source .venv/bin/activate

After successful activation, the environment name usually appears in the terminal prompt:

(.venv)

At this point, when you run Python and pip commands, the interpreter and package manager in the current virtual environment are usually used first.

4. Exit the virtual environment

Run:

deactivate

After exiting, the terminal returns to the original environment.

5. Delete the virtual environment

A virtual environment is essentially a directory.

When it is no longer needed, you can exit the environment and then delete the .venv folder.

macOS and Linux:

rm -rf .venv

Windows CMD:

rmdir /s /q .venv

PowerShell:

Remove-Item -Recurse -Force .venv

Deleting the virtual environment does not delete the project source code, but it does delete the dependencies installed in that environment; you can recreate it when needed.

VIII. Managing Python dependencies with pip

The Python standard library provides pip for installing, upgrading, and uninstalling third-party packages.

1. Check whether pip is installed

python -m pip --version

If you're using python3:

python3 -m pip --version

It is recommended to use:

python -m pip

rather than using this directly:

pip

because python -m pip explicitly specifies which Python interpreter runs pip, reducing environment confusion when multiple Python versions coexist.

2. Install third-party libraries

For example, to install requests:

python -m pip install requests

Install a specific version:

python -m pip install requests==2.32.3

Install multiple dependencies:

python -m pip install requests numpy pandas

3. View installed dependencies

python -m pip list

View detailed information about a package:

python -m pip show requests

4. Upgrade dependencies

python -m pip install --upgrade requests

Upgrade pip:

python -m pip install --upgrade pip

5. Uninstall dependencies

python -m pip uninstall requests

6. Manage dependencies with requirements.txt

You can export the list of packages in the current environment with the following command:

python -m pip freeze > requirements.txt

The generated file may look similar to:

requests==2.32.3
urllib3==2.2.3
certifi==2024.8.30
charset-normalizer==3.4.0
idna==3.10

To install these dependencies in another environment:

python -m pip install -r requirements.txt

Note that pip freeze records the packages installed in the current environment, and may include development tools and indirect dependencies.

For large projects, it is advisable to further distinguish direct dependencies, development dependencies, and locked dependencies.

9. Managing Projects with pyproject.toml

Modern Python projects usually use pyproject.toml as the project configuration file.

It can describe project metadata, dependencies, the build system, and tool configuration.

1. Basic Structure of pyproject.toml

For example:

[project]
name = "python-demo"
version = "0.1.0"
description = "Python 示例项目"
requires-python = ">=3.12"
dependencies = [
    "requests>=2.32",
    "pandas>=2.2"
]

[project.optional-dependencies]
dev = [
    "pytest",
    "ruff",
    "mypy"
]

[build-system]
requires = ["setuptools>=68"]
build-backend = "setuptools.build_meta"