Docker+DRF环境安装uWSGI报错:AttributeError: os模块无uname属性
解决Windows环境下安装uWSGI出现的AttributeError问题
在执行pip install -r requirements.txt --use-deprecated=legacy-resolver安装依赖时,安装uWSGI==2.0.21触发以下错误:
error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [8 lines of output] Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "C:\Users\jia52\AppData\Local\Temp\pip-install-2eb55qko\uwsgi\setup.py", line 3, in <module> import uwsgiconfig as uc File "C:\Users\jia52\AppData\Local\Temp\pip-install-2eb55qko\uwsgi\uwsgiconfig.py", line 8, in <module> uwsgi_os = os.uname()[0] AttributeError: module 'os' has no attribute 'uname' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> uWSGI
问题原因
uWSGI官方版本不支持Windows系统,os.uname()是Unix/Linux系统专属的系统信息获取方法,Windows系统的Python标准库中不存在该属性,导致安装过程中触发报错。
解决方案
方案1:使用Windows兼容的uWSGI分支
修改requirements.txt,将uWSGI的安装源替换为支持Windows的fork版本:
uWSGI @ git+https://github.com/unbit/uwsgi.git#egg=uWSGI
也可以直接执行以下命令单独安装:
pip install git+https://github.com/unbit/uwsgi.git#egg=uWSGI
方案2:替换为Windows原生支持的WSGI服务器
若无需强制使用uWSGI,可替换为跨平台或Windows原生支持的WSGI服务器:
- Waitress:纯Python实现的跨平台WSGI服务器,在requirements.txt中添加:
waitress==2.1.2 - Gunicorn:若使用WSL(Windows子系统Linux),可直接使用标准Gunicorn,无需修改依赖。
方案3:基于Docker容器部署(推荐)
鉴于你已使用Docker,直接在Linux镜像环境中部署uWSGI是最优解:
- Dockerfile选择Linux基础镜像(如
python:3.10-slim) - 在容器内执行
pip install -r requirements.txt,Linux环境下uWSGI可正常安装运行 - 容器化部署能统一开发与生产环境,彻底规避跨平台兼容问题
内容的提问来源于stack exchange,提问作者Jia Kwon
相关产品推荐
相关产品推荐

