Windows环境下通过pip从本地服务器安装自研Python包失败求助
问题:局域网内从本地服务器向Windows机器安装自研Python包失败
操作步骤
简要流程
- 创建包含
setup.py的自定义Python包 - 生成wheel文件
- 将wheel文件部署到本地服务器并通过HTTP托管
- 修改Windows客户端
pip.ini配置 - 执行安装命令时触发错误
详细操作
- 编写
setup.py文件:
from setuptools import setup, find_packages # Define package metadata name = 'sptools' version = '0.1' author = 'author' author_email = 'email' description = 'description' url = 'url' # Define package dependencies install_requires = [ 'requests==2.31.0', 'seaborn==0.13.0', 'torch==2.0.0', 'tqdm==4.65.0', 'utils==1.0.1', 'visualize==0.5.1' ] # Define packages to include (you can use find_packages()) packages = find_packages() # Define classifiers for your package classifiers = [ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'License :: Internal', 'Programming Language :: Python :: 3.10', ] # Define the setup configuration setup( name=name, version=version, author=author, author_email=author_email, description=description, url=url, install_requires=install_requires, packages=packages, classifiers=classifiers, )
- 生成wheel文件
sptools-0.1-py3-none-any.whl,本地安装正常 - 将wheel文件移至服务器
~/packages/sptools-0.1-py3-none-any.whl路径,通过python -m http.server托管,可通过wget http://[server_IP:port]/sptools-0.1-py3-none-any.whl手动下载 - 修改Windows客户端
pip.ini配置:
[global] no-cache-dir = true index-url = http://[ip:port]/ trusted-host = [ip] [install] trusted-host = pypi.org pypi.python.org pypi.ngc.nvidia.com
- 尝试安装命令:
pip install sptools # 或 pip install --trusted-host [ip] sptools
错误信息
客户端报错
ERROR: Could not find a version that satisfies the requirement sptools (from versions: none) ERROR: No matching distribution found for sptools
服务器日志
[client-ip] - - [11/Oct/2023 17:56:38] code 404, message File not found [client-ip] - - [11/Oct/2023 17:56:38] "GET /sptools/ HTTP/1.1" 404 - [client-ip] - - [11/Oct/2023 17:56:39] code 404, message File not found [client-ip] - - [11/Oct/2023 17:56:39] "GET /pip/ HTTP/1.1" 404 -
已排查内容
- 在conda环境内外均尝试过安装
- 确认包名完全一致
- 使用公开wheel包测试仍出现相同问题
- 手动下载wheel文件后本地安装正常
- 通过
pip config list确认客户端配置已生效
解决建议
问题核心:pip默认会向index-url指向的地址请求PyPI规范的索引页面(如/simple/sptools/),但仅用python -m http.server托管单个wheel文件时,没有生成符合要求的索引结构,导致pip无法识别包的元数据,触发404错误。
方案1:生成PyPI兼容的索引目录结构
在服务器的~/packages目录下执行以下命令:
# 安装索引生成工具 pip install twine # 生成simple格式的包索引 python -m pip index generate --directory .
执行后,目录下会生成simple文件夹,其中包含sptools子目录和对应的索引页面,pip可正常识别包信息。
方案2:修改客户端pip.ini配置
将index-url指向生成的simple索引地址,同时统一trusted-host配置:
[global] no-cache-dir = true index-url = http://[ip:port]/simple/ trusted-host = [ip] [install] trusted-host = [ip] pypi.org pypi.python.org pypi.ngc.nvidia.com
注意替换[ip:port]为实际服务器IP和端口。
方案3:直接指定wheel文件URL安装(无需生成索引)
如果不想搭建索引结构,可直接通过完整URL安装:
pip install http://[ip:port]/sptools-0.1-py3-none-any.whl --trusted-host [ip]
内容的提问来源于stack exchange,提问作者Eduard Barnoviciu
相关产品推荐
相关产品推荐

