pip 10报错:找不到pip.req模块及get-pip.py安装异常咨询
Hey there! Let's break down your two issues one by one and get them sorted out.
This message is actually letting you know that pip is already installed on your system, and it’s already at the latest version (10.0.0) that the get-pip.py script is trying to set up. So there’s no need to reinstall it unless you have a specific reason to overwrite the existing installation (like if your current pip is corrupted).
If you do need to force a reinstall, you can run the script with the --ignore-installed flag to bypass the "already up-to-date" check:
python get-pip.py --ignore-installed pip
This error pops up because starting with pip 10.0.0, the pip.req module was completely removed—it’s no longer part of pip’s public API. The old parse_requirements function you were using in your setup.py has been moved to an internal module, and relying on internal modules isn’t recommended since they can change without warning in future pip updates.
Here are a few reliable fixes, ordered by how robust they are:
Option 1: Manually parse your requirements.txt (most recommended)
Skip pip’s internal functions entirely by reading and parsing the requirements file directly. This approach is simple and avoids any pip version compatibility headaches:# Inside your setup.py with open('requirements.txt') as f: # Filter out comments and empty lines requirements = [line.strip() for line in f if line.strip() and not line.startswith('#')] setup( # Your other setup parameters... install_requires=requirements )Option 2: Use pip's internal module (not recommended, but works for pip 10+)
If you absolutely need the more robust parsing thatparse_requirementsoffered (like handling optional dependencies), you can use the internalpip._internal.reqmodule—but keep in mind this might break in future pip versions:# Inside your setup.py from pip._internal.req import parse_requirements from pip._internal.network.session import PipSession # Parse requirements with a valid session object reqs = parse_requirements('requirements.txt', session=PipSession()) requirements = [str(req.requirement) for req in reqs] setup( # Your other setup parameters... install_requires=requirements )Option 3: Use a third-party library
For a maintained, public API to parse requirements, you can use libraries likerequirements-parserorpip-tools. Here’s how it works withrequirements-parser:- First install the library:
pip install requirements-parser - Then use it in your setup.py:
from requirements_parser import parse reqs = parse('requirements.txt') requirements = [str(req) for req in reqs] setup( # Your other setup parameters... install_requires=requirements )
- First install the library:
内容的提问来源于stack exchange,提问作者codingCoffee

