如何实现Python包安装时自动获取并安装freeglut依赖?
解决Python包自动安装带FreeGLUT的PyOpenGL依赖问题
先理清楚你遇到的几个核心问题:
- 用
git+前缀指向单个WHL文件导致pip解析错误 - 新版pip已经废弃了
dependency_links参数 - 跨平台下FreeGLUT依赖的安装差异
1. 为什么你的WHL链接会报错?
你之前写的pyopengl @ git+https://raw.githubusercontent.com/.../PyOpenGL-3.1.5-cp38-cp38-win32.whl格式不对——git+前缀是用来指定Git仓库地址的,而你链接的是单个WHL文件。pip会尝试用Git去克隆这个文件,自然会出现路径解析错误(也就是你看到的TypeError: stat: path should be string...)。
正确的写法是直接去掉git+前缀,用WHL文件的原始URL即可。
2. 废弃的dependency_links怎么替代?
从pip 19.0开始,dependency_links就被正式废弃了,现在推荐用PEP 508环境标记来指定平台/版本特定的依赖,直接写在install_requires里。
3. 修改后的setup.py示例
下面是适配跨平台需求的setup.py写法,既支持Windows自动安装带FreeGLUT的预编译WHL,也支持非Windows平台安装标准PyOpenGL(需要提前装系统级FreeGLUT):
from setuptools import setup, find_packages import os with open("README.md", "r") as fh: long_description = fh.read() setup( name="pyunity", version="0.0.1", author="Ray Chen", author_email="tankimarshal2@gmail.com", description="A Python implementation of the Unity Engine", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/rayzchen/PyUnity", # 修正原URL的拼写错误 packages=find_packages(), classifiers=[ "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "License :: OSI Approved :: MIT License", "Operating System :: Microsoft :: Windows :: Windows 10", "Operating System :: POSIX :: Linux", "Operating System :: MacOS", ], install_requires=[ "glfw", "pygame", # Windows平台:指定预编译的带FreeGLUT的WHL(匹配Python3.8) 'pyopengl @ https://raw.githubusercontent.com/rayzchen/PyUnity/master/PyOpenGL-3.1.5-cp38-cp38-win32.whl ; sys_platform == "win32" and python_version == "3.8"', 'pyopengl-accelerate @ https://raw.githubusercontent.com/rayzchen/PyUnity/master/PyOpenGL_accelerate-3.1.5-cp38-cp38-win32.whl ; sys_platform == "win32" and python_version == "3.8"', # 如需支持更多Python版本,添加对应版本的WHL链接 # 'pyopengl @ https://xxx/PyOpenGL-3.1.5-cp37-cp37m-win32.whl ; sys_platform == "win32" and python_version == "3.7"', # 非Windows平台:安装标准PyOpenGL,需提前装系统级FreeGLUT 'PyOpenGL ; sys_platform != "win32"', 'PyOpenGL-accelerate ; sys_platform != "win32"', ], python_requires='>=3.7', )
4. 非Windows平台的额外说明
对于Linux或macOS用户,需要先安装系统级的FreeGLUT依赖:
- Ubuntu/Debian:
sudo apt-get install freeglut3-dev - macOS:
brew install freeglut
安装完系统依赖后,pip会自动从PyPI拉取标准PyOpenGL包,此时PyOpenGL就能正常调用FreeGLUT了。
5. 其他注意事项
- 确保你上传到GitHub的WHL文件是正确的(对应Python版本、架构),否则Windows用户安装时会出现不兼容错误。
- 如果要支持更多Python版本(比如3.9+),你需要从Gohlke的网站下载对应版本的WHL,上传到GitHub后添加对应的依赖条目。
内容的提问来源于stack exchange,提问作者Ray
相关产品推荐
相关产品推荐

