如何在用pip安装tarball时避免文件未找到错误?
我正在创建一个本地的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'
)
1 个回答
1
问题:bin/zen_handler
文件在发行包中缺失
问题是,你的 setup.py
文件要求使用 bin/zen_handler
这个文件,但在你生成的压缩包里却找不到这个文件。
事情是怎么发生的?
- 你在源代码目录里有
bin/zen_handler
这个文件。 - 你的
setup.py
文件定义了创建发行包的条件。 - 你创建了发行包。
- 你用这个发行包来安装程序。
- 结果发现发行包里没有
bin/zen_handler
,所以安装失败了。
你需要检查一下,gz
文件里是否包含 bin/zen_handler
。我猜这个文件是缺失的。
原因是,bin/zen_handler
这个文件并不是 Python 包 zen_handler
的一部分,所以它没有被打包进发行包里。
解决方案
将这个非 Python 文件声明为发行包的一部分
找到 setup.py
中的选项,将 bin/zen_handler
声明为发行包的一部分。这可能意味着你需要编辑 MANIFEST.in
文件(把这个文件添加进去)和/或使用参数 include_package_data
。
(推荐)使用 entry_points
安装脚本
与其使用参数 scripts
,不如使用 entry_point
,具体可以参考 自动脚本创建。因为这个方法完全依赖于 Python 包的源代码,所以不需要外部文件的存在。
我推荐这个解决方案,尽管它需要你对代码做一些小修改。