杰瑞科技汇

Python build install命令如何正确使用?

Of course! The phrase python build install refers to the process of taking a Python project's source code and installing it onto your system so you can use it as a library or a command-line tool.

Python build install命令如何正确使用?-图1
(图片来源网络,侵删)

This is a fundamental task in Python development. Let's break it down into the modern, recommended approach and the older, more manual way.


The Modern & Recommended Way: pip in a Virtual Environment

This is the standard, safest, and most reliable method. It uses pip (Python's installer) to handle the build and installation process in an isolated environment.

Step 1: Create a Virtual Environment (Highly Recommended)

A virtual environment creates an isolated space for your project's dependencies. This prevents conflicts between different projects and avoids "polluting" your global Python installation.

  1. Navigate to your project directory:

    Python build install命令如何正确使用?-图2
    (图片来源网络,侵删)
    cd /path/to/your/project
  2. Create the virtual environment: It's common to name it venv.

    # For Python 3
    python3 -m venv venv
    • If you have multiple Python versions, you can be explicit: python3.10 -m venv venv.
    • On Windows, you might need to use py: py -m venv venv.
  3. Activate the virtual environment:

    • On macOS/Linux:
      source venv/bin/activate
    • On Windows (Command Prompt):
      .\venv\Scripts\activate
    • On Windows (PowerShell):
      .\venv\Scripts\Activate.ps1

      You'll know it's active because your command prompt will be prefixed with (venv).

Step 2: Install the Project in "Editable" Mode

This is the best way to install a project you are actively developing. It creates a "link" from your site-packages to your project's source code. Any changes you make to the source code are immediately available without needing to reinstall.

Python build install命令如何正确使用?-图3
(图片来源网络,侵删)
  1. Ensure your project has a setup.py or, even better, a pyproject.toml file. This file tells pip how to build and install the project.

  2. Run the install command from your project's root directory:

    # The -e flag stands for "editable"
    pip install -e .
    • pip: The Python Package Installer.
    • install: The command to install a package.
    • -e: The "editable" flag.
    • Tells pip to look for the build configuration (setup.py or pyproject.toml) in the current directory.

What happens when you run pip install -e .?

  1. Build: pip reads your setup.py or pyproject.toml to understand what the project is and what it needs. It then builds any necessary C extensions or compiles the package.
  2. Install: pip installs the built package into your currently active virtual environment's site-packages directory.
  3. Link (Editable Mode): Crucially, in editable mode, it also creates a special .egg-link file that points back to your source directory. It also installs any "entry points" (like command-line tools) into the virtual environment's bin (or Scripts on Windows) directory.

The Old Manual Way (Not Recommended for Development)

Before pip became so sophisticated, developers had to run the build and install steps separately. You might still see this in some older documentation or CI scripts.

This process relies on the setuptools library.

Step 1: Build the Package

This command creates a distributable archive (a .tar.gz file) from your source code.

  1. Navigate to your project's root directory (where setup.py is located).
  2. Run the build command:
    python setup.py build

    This creates a build/ directory containing the compiled package.

Step 2: Install the Package

This command takes the built package from the build/ directory and copies it to your Python site-packages directory.

  1. Run the install command:
    python setup.py install
    • Warning: This installs the package into your global Python environment. This is bad practice because it can lead to dependency conflicts between projects. Always prefer the pip install -e . method.

What if My Project Uses pyproject.toml?

Modern Python projects are increasingly using pyproject.toml instead of setup.py. This file is part of the PEP 518 standard and is more declarative.

The great news is that pip handles pyproject.toml automatically. The command is exactly the same:

pip install -e .

pip will read the [build-system] section of your pyproject.toml to figure out which build tool (like setuptools, flit, or poetry) to use. You don't need to know or care about the details.


Summary: Which Command to Use?

Your Goal Recommended Command Why?
Developing a project pip install -e . Installs in editable mode into a virtual environment. Changes are reflected immediately.
Installing a dependency from PyPI pip install <package_name> Installs a pre-built package from the Python Package Index.
Installing a released version of a local project pip install . (without -e) Installs a non-editable version. Useful for testing a release.
Following an old tutorial python setup.py build && python setup.py install Avoid this for development. It's outdated and can mess up your global Python installation.
分享:
扫描分享到社交APP
上一篇
下一篇