杰瑞科技汇

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

Of course! The command you're looking for is almost certainly python setup.py install, but there's a much better, modern alternative.

Let's break it down.


The Old Way: python setup.py install

This is the traditional way to install a Python package from source code that you've downloaded.

How it Works

  1. A package contains a file named setup.py. This is a Python script that contains metadata about the package (its name, version, author, dependencies, etc.) and instructions on how to build and install it.
  2. When you run python setup.py install, you are telling Python to execute this script.
  3. The script then builds the package and installs its files into your Python environment's site-packages directory.

When to Use It

You should only use this method in specific situations:

  • Installing from a source download: You've downloaded a .tar.gz or .zip file from a source repository (like PyPI or a GitHub releases page) and it doesn't have a pyproject.toml file.
  • Legacy systems: You are working on an old project or system that doesn't support modern packaging tools.
  • Development: You are actively developing a package and want to install it in "editable" mode (python setup.py develop), though even this is now superseded by pip install -e ..

⚠️ Important Problems with python setup.py install

  1. It's Outdated: The setuptools library, which setup.py relies on, has been superseded by more modern standards like PEP 517 and PEP 518. These new standards use a pyproject.toml file instead of setup.py.
  2. It Breaks Environments: This command installs packages directly into your system's global Python site-packages. This is a bad practice because it can lead to dependency conflicts between different projects.
  3. It Ignores Virtual Environments: If you have a virtual environment active, it might still try to install to the global location, bypassing the environment entirely.
  4. It Doesn't Handle Dependencies Well: It doesn't automatically fetch and install the package's dependencies (you have to do that manually).

The Modern & Correct Way: pip install .

The standard and recommended way to install a package (from a local directory or a source archive) is by using pip.

Python setup install命令如何正确使用?-图1

How it Works

  1. You navigate your terminal to the root directory of the package you want to install (the directory that contains setup.py or pyproject.toml).
  2. You run pip install . (the tells pip to install the package from the current directory).
  3. pip is smart. It will detect the build system (either the old setuptools or the new build standard) and build the package correctly.
  4. It then installs the package into the currently active Python environment.

When to Use It

This is the default command you should use for almost every scenario:

  • Installing a package you've cloned from GitHub.
  • Installing a package from a local source code directory.
  • Installing a package from a downloaded .tar.gz or .zip file (you can point pip directly to the file: pip install /path/to/package-1.0.tar.gz).

Advantages over python setup.py install

  • Environment-Aware: It correctly installs into your active virtual environment, preventing global pollution.
  • Handles Dependencies: It automatically reads the package's dependencies from setup.py or pyproject.toml and installs them first.
  • Future-Proof: It works with both old (setup.py) and new (pyproject.toml) packaging standards.
  • Standard Practice: This is the community-accepted, modern way to do it.

Summary: python setup.py install vs. pip install .

Feature python setup.py install (Old Way) pip install . (Modern Way)
Primary Tool setuptools script pip (Python's Package Installer)
Installation Target Often global, can ignore virtual environments Correctly targets the active virtual environment
Dependency Handling Manual; requires user to install dependencies first Automatic; reads and installs dependencies
Standards Outdated (setuptools-only) Supports both old (setup.py) and new (pyproject.toml)
Recommendation Avoid. Use only for legacy systems or specific source archives. Highly Recommended. The standard for all modern Python projects.

Step-by-Step Example (The Modern Way)

Let's say you want to install a package called requests directly from its GitHub source code.

  1. Clone the repository:

    Python setup install命令如何正确使用?-图2

    git clone https://github.com/psf/requests.git
  2. Navigate into the project directory:

    cd requests

    (You'll see files like setup.py, pyproject.toml, README.rst, etc.)

  3. (Recommended) Create and activate a virtual environment: This isolates the package's dependencies from your system's Python.

    Python setup install命令如何正确使用?-图3

    # Create the environment
    python -m venv .venv
    # Activate it (the command differs for OS)
    # On macOS/Linux:
    source .venv/bin/activate
    # On Windows:
    .venv\Scripts\activate

    Your terminal prompt should now change to show (.venv).

  4. Install the package using pip:

    pip install .

    You will see output from pip as it builds the package and installs it, along with its dependencies like charset-normalizer, idna, and urllib3, into your .venv environment.

You have now successfully and correctly installed the package!

分享:
扫描分享到社交APP
上一篇
下一篇