在PEX中使用python脚本执行时出现pyyaml依赖项错误

2024-04-29 04:46:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试使用PEX打包和执行脚本时遇到以下错误

Failed to execute PEX file. Needed macosx_10_14_x86_64-cp-38-cp38 compatible dependencies for:
 1: pyyaml
    Required by:
      app==0.0.1
    But this pex only contains:
      PyYAML-5.3.1-cp39-cp39-macosx_10_14_x86_64.whl

下面是我打包文件的步骤:

执行以下命令以生成zip文件

pex requests 'XlsxWriter==1.3.7' 'urllib3==1.26.2' 'PyYAML==5.3.1' ./app -o app.pex --python-shebang '#!/usr/bin/env python3'

使用调试日志执行时,我看到以下错误

(base)$ PEX_VERBOSE=9 ./app.pex

pex: Activating
 PEX virtual environment from ./app.pex :: Searching dependency cache: /Users[PATH]/app.pex/pex: Activating
 PEX virtual environment from ./app.pex :: Searching dependency cache: /Users[PATH]/app.pex/pex: Activating
 PEX virtual environment from ./app.pex :: Searching dependency cache: /Users[PATH]/app.pex/pex: Activating
 PEX virtual environment from ./app.pex :: Searching dependency cache: /Users[PATH]/app.pex/pex: Activating
 PEX virtual environment from ./app.pex :: Searching dependency cache: /Users[PATH]/app.pex/pex: Activating
 PEX virtual environment from ./app.pex :: Searching dependency cache: /Users[PATH]/app.pex/pex: Activating
 PEX virtual environment from ./app.pex :: Searching dependency cache: /Users[PATH]/app.pex/pex: Activating
 PEX virtual environment from ./app.pex :: Resolving pyyaml from [Requirement.parse('PyYAML==5.3.1; python_full_version != "3.0.*" and python_full_version != "3.1.*" and python_full_version pex: Failed to resolve a requirement: The 'pyyaml' distribution was not found and is required by the application                                                                                                                                                                                                            
pex: Failed to resolve a requirement: The 'PyYAML' distribution was not found and is required by app                        
pex: Activating
 PEX virtual environment from ./app.pex :: Resolving chardet from [Requirement.parse('chardet==4.0.0; python_full_version != "3.0.*" and python_full_version != "3.1.*" and python_full_versiopex: Activating
 PEX virtual environment from ./app.pex :: Resolving idna from [Requirement.parse('idna==2.10; python_full_version != "3.0.*" and python_full_version != "3.1.*" and python_full_version != "3pex: Activating
 PEX virtual environment from ./app.pex :: Resolving requests from [Requirement.parse('requests==2.25.1; python_full_version != "3.0.*" and python_full_version != "3.1.*" and python_full_verpex: Activating
 PEX virtual environment from ./app.pex :: Resolving urllib3 from [Requirement.parse('urllib3==1.26.2; python_full_version != "3.0.*" and python_full_version != "3.1.*" and python_full_versipex: Unresolved requirements:

环境:



$ python --version
Python 2.7.16


$ python3 --version
Python 3.9.1


$ python2 --version
bash: python2: command not found


$ pip --version
bash: /usr/local/bin/pip: /usr/local/opt/python@3.8/bin/python3.8: bad interpreter: No such file or directory


$ pex --version
2.1.24
$ whereis python
/usr/bin/python

Tags: andpathfromappcacheenvironmentsearchingversion
2条回答

我通过将env改为3.9解决了这个问题

pex requests 'XlsxWriter==1.3.7' 'urllib3==1.26.2' 'PyYAML==5.3.1' ./app -o app.pex  python-shebang '#!/usr/bin/env python3.9'

从您的环境数据来看,您必须安装python3.8,然后执行以下操作:

python3.8 -m pip install pyyaml

您可能首先在您的计算机上安装了python3.8,然后用python3.8安装了pex,然后卸载了python3.8。所以,现在只剩下python3.9了,但是pex仍然配置为使用python3.8。pex现在为python3.8查找pyyaml,但只为python3.9查找pyyaml。另一种解决方案是卸载您现在拥有的pex,然后使用以下命令重新安装:

python3.9 -m pip install pex

相关问题 更多 >