Skip to main content
 首页 » 编程设计

python之如何避免使用 pip 安装 tarball 时出现文件未找到错误

2025年05月04日8lvdongjie

我正在构建一个本地 python 包

cd <source dir> 
python ./setup.py sdist 

当我尝试使用 pip 安装它时,它会尝试删除不存在的文件,但失败了。

pip install --verbose dist/pl_zenoss_handler-0.1.1.tar.gz 
Unpacking ./dist/pl_zenoss_handler-0.1.1.tar.gz 
  Running setup.py (path:/tmp/pip-QohNov-build/setup.py) egg_info for package from file:///Users/travis.bear/p4/depot/service/python/_pl_zenoss_handler/dist/pl_zenoss_handler-0.1.1.tar.gz 
    running egg_info 
    creating pip-egg-info/pl_zenoss_handler.egg-info 
    writing pip-egg-info/pl_zenoss_handler.egg-info/PKG-INFO 
    writing top-level names to pip-egg-info/pl_zenoss_handler.egg-info/top_level.txt 
 
<... much output deleted for brevity ... > 
 
creating build/scripts-2.7 
 
error: file '/private/tmp/pip-QohNov-build/bin/zen_handler' does not exist 
 
---------------------------------------- 
Cleaning up... 
Command /Users/travis.bear/venv/zenoss/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-QohNov-build/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-uIoyIG-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/travis.bear/venv/zenoss/include/site/python2.7 failed with error code 1 in /tmp/pip-QohNov-build 
Exception information: 
Traceback (most recent call last): 
  File "/Users/travis.bear/venv/zenoss/lib/python2.7/site-packages/pip/basecommand.py", line 122, in main 
    status = self.run(options, args) 
  File "/Users/travis.bear/venv/zenoss/lib/python2.7/site-packages/pip/commands/install.py", line 279, in run 
    requirement_set.install(install_options, global_options, root=options.root_path) 
  File "/Users/travis.bear/venv/zenoss/lib/python2.7/site-packages/pip/req.py", line 1380, in install 
    requirement.install(install_options, global_options, *args, **kwargs) 
  File "/Users/travis.bear/venv/zenoss/lib/python2.7/site-packages/pip/req.py", line 699, in install 
    cwd=self.source_dir, filter_stdout=self._filter_install, show_stdout=False) 
  File "/Users/travis.bear/venv/zenoss/lib/python2.7/site-packages/pip/util.py", line 697, in call_subprocess 
    % (command_desc, proc.returncode, cwd)) 
InstallationError: Command /Users/travis.bear/venv/zenoss/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-QohNov-build/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-uIoyIG-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/travis.bear/venv/zenoss/include/site/python2.7 failed with error code 1 in /tmp/pip-QohNov-build 
 
Storing debug log for failure in /Users/travis.bear/.pip/pip.log 

这是 setup.py 文件:

from setuptools import setup 
 
readme = open('README.rst').read() 
history = open('HISTORY.rst').read().replace('.. :changelog:', '') 
 
setup( 
    name='pl_zenoss_handler', 
    version='0.1.1', 
    description='Sensu handler for Zenoss', 
    long_description=readme + '\n\n' + history, 
    author='Travis Bear', 
    author_email='<snip>', 
    url='<snip>', 
    packages=[ 
        'zen_handler', 
    ], 
    scripts=['bin/zen_handler'], 
    install_requires=[ 
    ], 
    license="BSD", 
    keywords='zenoss sensu' 
) 

请您参考如下方法:

问题:分发包中缺少bin/zen_handler文件

问题是,您的 setup.py 要求使用 bin/zen_handler 中的文件,而在您的分发 gz 文件中它丢失了。

进展如何?

  1. 你的源代码树中有 bin/zen_handler
  2. 你的setup.py定义了创建分发包的条件
  3. 您创建分发包
  4. 您使用分发包安装程序
  5. 分发包不包含 bin/zen_handler 并且失败。

您应该检查gz 文件是否包含bin/zen_handler。我假设它不存在。

原因是,因为它不是 python 包 zen_handler 本身的一部分,所以它没有打包到发行版中。

决议

将此非 python 文件声明为分发的一部分

找到 setup.py 的选项,它甚至会将 bin/zen_handler 声明为分发包的一部分。这可能意味着编辑 MANIFEST.in 文件(在其中添加该文件)和/或使用参数 include_package_data。

(首选)使用entry_points安装脚本

不使用参数脚本,而是使用entry_point,如Automatic Script Creation 中所述。 .由于这是使用纯python包源代码,所以不需要外部文件的存在。

我推荐此解决方案,即使它只需要对您的代码进行少量修改。