杰瑞科技汇

Python easy install如何正确使用?

Of course! Let's break down easy_install, what it is, how to use it, and most importantly, why you should avoid it.

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

What is easy_install?

easy_install is a command-line tool that was the original package installer for Python. It was part of Setuptools, a library for packaging, distributing, and installing Python projects.

Its main job was to find packages on the Python Package Index (PyPI) and download/install them for you.

Key takeaway: easy_install is the predecessor to the modern and highly recommended tool, pip.


How to Use easy_install (The Old Way)

If you have an older Python installation or have Setuptools explicitly installed, you can use it like this.

Python easy install如何正确使用?-图2
(图片来源网络,侵删)

Basic Installation

To install a package from PyPI:

# Install a single package
easy_install requests
# Install a specific version
easy_install "requests==2.25.1"
# Install from a different source (like a URL)
easy_install http://example.com/my-package.tar.gz

Uninstalling a Package

This is one of easy_install's biggest weaknesses. It's not straightforward.

# You have to find the package and manually delete it
# This command finds the path and then you delete it
easy_install -m requests  # This just marks it for uninstall, doesn't do it

You would then have to manually find the package in your Python's site-packages directory and delete its files. This is cumbersome and error-prone.


Why You Should NOT Use easy_install Anymore

While easy_install was revolutionary in its time, it has significant drawbacks that led to the creation of pip. Here’s a comparison to show why pip is vastly superior.

Python easy install如何正确使用?-图3
(图片来源网络,侵删)
Feature easy_install pip Winner
Dependency Resolution Poor. Installs dependencies without checking for conflicts. Can easily break your environment. Excellent. Intelligently resolves and checks for conflicting dependencies. pip
Uninstalling Very Difficult. Requires manual deletion of files. No reliable command. Easy. pip uninstall <package> pip
Virtual Environments No built-in support. Works directly on the system Python, which is risky. Fully Integrated. pip is the standard tool for use inside venv and virtualenv. pip
Package Management Basic. Only installs. No way to list, upgrade, or check what's installed. Comprehensive. list, show, freeze, upgrade. pip
Source Control Limited. Can't install directly from Git, Mercurial, etc. Excellent. pip install git+https://... is standard practice. pip
Default in Python No. It was a separate install. Yes! pip is included by default with Python 3.4+ and Python 2.7.9+. pip

In short: easy_install is outdated, less reliable, and less powerful.


The Modern Way: Using pip

pip is now the standard and officially recommended package installer for Python. It's what you should use for all your Python projects.

How to Check if pip is Installed

Open your terminal or command prompt and run:

# For Python 3
pip --version
# For Python 2 (if you still need it)
pip2 --version

If you get an error, you may need to install it. For modern Python, it should already be there.

Basic pip Usage

# Install a package (this will also handle its dependencies correctly)
pip install requests
# Install a specific version
pip install "requests==2.25.1"
# Upgrade a package to the latest version
pip install --upgrade requests
# Uninstall a package (the easy_install way, but easy!)
pip uninstall requests
# List all installed packages
pip list
# Show details about a specific package
pip show requests
# Generate a requirements.txt file (crucial for sharing projects)
pip freeze > requirements.txt
# Install packages from a requirements.txt file
pip install -r requirements.txt

Summary and Final Recommendation

Tool Status Recommendation
easy_install Legacy / Deprecated Avoid. Use only if you are forced to maintain very old code.
pip Current Standard Use this for everything. It's more reliable, safer, and has all the features you need.

Best Practice Workflow

  1. Create a Virtual Environment: This isolates your project's dependencies from your system's Python.

    # For Python 3
    python -m venv my-project-env
    # Activate the environment
    # On Windows:
    my-project-env\Scripts\activate
    # On macOS/Linux:
    source my-project-env/bin/activate
  2. Use pip to Install Packages: Inside your active virtual environment, install your packages.

    pip install numpy pandas matplotlib
  3. Save Your Dependencies: Create a requirements.txt file to share your project setup.

    pip freeze > requirements.txt
  4. Collaborate: Others can set up the exact same environment by running:

    pip install -r requirements.txt
分享:
扫描分享到社交APP
上一篇
下一篇