Poetry安装报错:已指定Python3.10却仍调用Python2.7
问题描述
执行poetry install时触发报错:当前终端激活的Python版本为2.7.18,但项目pyproject.toml已明确指定Python依赖版本为^3.10,且本地已安装Python 3.10。报错详情如下:
poetry install Warning: In a future version of Poetry, PyPI will be disabled automatically if at least one custom source is configured with another priority than 'explicit'. In order to avoid a breaking change and make your pyproject.toml forward compatible, add PyPI explicitly via 'poetry source add pypi'. By the way, this has the advantage that you can set the priority of PyPI as with any other source. The currently activated Python version 2.7.18 is not supported by the project (^3.10). Trying to find and use a compatible version. Creating virtualenv my-pipelines-components-e6az2al8-py2.7 in /home/nairv/.cache/pypoetry/virtualenvs failed to query /usr/bin/python2.7 with code 1 err: ' File "/home/nairv/.local/lib/python3.7/site-packages/virtualenv/discovery/py_info.py", line 24 return list(OrderedDict.fromkeys(["", *os.environ.get("PATHEXT", "").lower().split(os.pathsep)])) ^ SyntaxError: invalid syntax '
项目pyproject.toml相关片段:
[tool.poetry] name = "my-pipelines-components" version = "0.1.0" description = "Pipelines Components" readme = "README.md" packages = [{ include = "src"}] [tool.poetry.dependencies] python = "^3.10"
疑问
为什么Poetry仍调用Python 2.7?如何移除该版本引用并修复问题?
原因分析
- Poetry运行环境不匹配:你的Poetry是安装在Python 2.7环境下的,导致它默认调用Python 2.7执行操作,无视项目指定的Python版本要求。
- PATH优先级问题:系统
PATH环境变量中,Python 2.7的路径排在Python 3.10之前,Poetry查找解释器时优先命中2.7版本。 - virtualenv兼容性冲突:报错中用到的
virtualenv是基于Python 3.7安装的,但被Python 2.7调用——Python 2.7不支持*解包语法,因此触发语法错误。
解决步骤
1. 重新安装Poetry到Python 3环境
先卸载Python 2.7环境下的Poetry:
pip uninstall poetry
用Python 3.10重新安装:
python3.10 -m pip install poetry
2. 指定Poetry使用Python 3.10创建虚拟环境
直接指定目标Python解释器路径:
poetry env use /usr/bin/python3.10
如果系统中python3指向3.10,也可以简化为:
poetry env use python3
3. 清理旧虚拟环境并重新执行安装
删除之前生成的Python 2.7虚拟环境:
poetry env remove python2.7
重新执行项目依赖安装:
poetry install
4. 调整系统PATH(可选)
如果希望终端默认优先使用Python 3.10,可修改PATH优先级。在~/.bashrc或~/.zshrc中添加:
export PATH="/usr/bin/python3.10:$PATH"
执行source ~/.bashrc(对应你使用的shell配置文件)使修改生效。
内容的提问来源于stack exchange,提问作者vinod827
相关产品推荐
相关产品推荐

