执行poetry install触发Python致命错误,无法创建venv
问题描述
执行poetry install时出现以下错误:
Could not find platform independent libraries <prefix> Could not find platform dependent libraries <exec_prefix> Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] Python path configuration: PYTHONHOME = (not set) PYTHONPATH = (not set) program name = '/Users/tok/Library/Application Support/pypoetry/venv/bin/python' isolated = 0 environment = 1 user site = 1 import site = 1 sys._base_executable = '/Users/tok/Library/Application Support/pypoetry/venv/bin/python' sys.base_prefix = '/Users/tok/anaconda3' sys.base_exec_prefix = '/Users/tok/anaconda3' sys.platlibdir = 'lib' sys.executable = '/Users/tok/Library/Application Support/pypoetry/venv/bin/python' sys.prefix = '/Users/tok/anaconda3' sys.exec_prefix = '/Users/tok/anaconda3' sys.path = [ '/Users/tok/anaconda3/lib/python39.zip', '/Users/tok/anaconda3/lib/python3.9', '/Users/tok/anaconda3/lib/lib-dynload', ] Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state: core initialized ModuleNotFoundError: No module named 'encodings' Current thread 0x00007ff84884a640 (most recent call first): <no Python frame>
原以为和Anaconda3有关,卸载Anaconda3后错误仍存在。使用Python3.9环境,对poetry不熟悉,求解决建议。
更新
尝试设置PYTHONHOME和PYTHONPATH后仍报错,同时尝试创建项目venv、卸载重装Python,问题依旧:
Python path configuration: PYTHONHOME = '/Users/tok/anaconda3' PYTHONPATH = '/Users/tok/anaconda3/bin' program name = '/Users/tok/Library/Application Support/pypoetry/venv/bin/python' isolated = 0 environment = 1 user site = 1 import site = 1 sys._base_executable = '/Users/tok/Library/Application Support/pypoetry/venv/bin/python' sys.base_prefix = '/Users/tok/anaconda3' sys.base_exec_prefix = '/Users/tok/anaconda3' sys.platlibdir = 'lib' sys.executable = '/Users/tok/Library/Application Support/pypoetry/venv/bin/python' sys.prefix = '/Users/tok/anaconda3' sys.exec_prefix = '/Users/tok/anaconda3' sys.path = [ '/Users/tok/anaconda3/bin', '/Users/tok/anaconda3/lib/python39.zip', '/Users/tok/anaconda3/lib/python3.9', '/Users/tok/anaconda3/lib/python3.9/lib-dynload', ] Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state: core initialized ModuleNotFoundError: No module named 'encodings' Current thread 0x00007ff8596ec640 (most recent call first): <no Python frame>
解决建议
1. 彻底清理Anaconda残留配置
卸载Anaconda后,系统环境变量或poetry配置仍残留Anaconda路径,这是核心问题:
- 打开
~/.bash_profile、~/.zshrc、~/.profile等shell配置文件,删除所有和Anaconda相关的行(比如PATH中包含/Users/tok/anaconda3/bin的语句,以及PYTHONHOME、PYTHONPATH的配置) - 执行
source ~/.zshrc(或对应你的shell配置文件)使变更生效 - 删除Anaconda残留目录:
rm -rf /Users/tok/anaconda3
2. 重置Poetry自身的虚拟环境
Poetry的全局虚拟环境绑定了已卸载的Anaconda Python,需重新安装Poetry:
- 卸载当前Poetry:
curl -sSL https://install.python-poetry.org | python3 - --uninstall - 用系统Python3.9重新安装Poetry:
curl -sSL https://install.python-poetry.org | python3.9 - - 验证Poetry使用的Python路径:
poetry run which python,输出应指向你的系统Python3.9(比如/usr/local/bin/python3.9或/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9)
3. 重新配置项目的Poetry环境
进入项目目录,重置项目虚拟环境:
- 删除项目下的
.venv目录(如果存在) - 指定Poetry使用正确的Python3.9:
poetry env use python3.9 - 重新执行
poetry install
4. 验证系统Python3.9完整性
确保系统安装的Python3.9本身无问题:
- 执行
python3.9 -c "import encodings; print(encodings.__file__)",若能正常输出路径,说明Python本身没问题 - 若此步骤报错,说明Python3.9安装不完整,重新下载官方Python3.9安装包覆盖安装
内容的提问来源于stack exchange,提问作者tokuyou-li
相关产品推荐
相关产品推荐

