已安装gunicorn 21.2.0,但Poetry因版本不匹配无法执行命令
解决Poetry锁依赖时gunicorn版本不匹配问题
问题现象
执行poetry lock时报错:
theiago@Theiago:~/kuhlenbier_webapi_tap$ poetry lock Updating dependencies Resolving dependencies... (2.7s) Because kuhlenbier-api depends on gunicorn (^20.2.0) which doesn't match any versions, version solving failed.
但全局环境中已安装gunicorn 21.2.0:
theiago@Theiago:~/kuhlenbier_webapi_tap$ gunicorn --version gunicorn (version 21.2.0)
解决步骤
- 修正版本约束:打开项目根目录的
pyproject.toml,找到gunicorn的依赖配置行。^20.2.0的约束仅允许20.x.x系列版本,21.x.x不在兼容范围内。如果项目支持21.x版本,可将约束改为>=20.2.0(允许20.2.0及以上所有版本)或^20.2.0 || ^21.0.0(同时兼容20.x和21.x系列)。 - 检查虚拟环境版本:Poetry默认使用独立虚拟环境,全局安装的gunicorn不影响虚拟环境内的依赖。执行
poetry env info查看当前虚拟环境路径,再用poetry run gunicorn --version确认虚拟环境内的版本。若版本不符,执行poetry add gunicorn@^20.2.0安装指定版本后,重新运行poetry lock。 - 清理Poetry缓存:缓存的版本信息可能过期,执行
poetry cache clear pypi --all清理缓存,之后重新运行poetry lock。 - 确认依赖源可用性:检查Poetry的PyPI源是否能正常获取gunicorn版本。执行
poetry config repositories查看配置,若使用非官方源,确认该源同步了gunicorn 20.x版本;若有问题,切换回官方源:poetry config repositories.pypi https://pypi.org/simple。
内容的提问来源于stack exchange,提问作者Thiago Souza
相关产品推荐
相关产品推荐

