【问题标题】:pip installing data files to the wrong placepip 将数据文件安装到错误的位置
【发布时间】:2013-12-16 09:58:46
【问题描述】:

包的来源是here

我正在通过以下方式从索引安装包:

easy_install hackertray
pip install hackertray

easy_installimages/hacker-tray.png 安装到以下文件夹:

/usr/local/lib/python2.7/dist-packages/hackertray-1.8-py2.7.egg/images/

同时,pip 将其安装到:

/usr/local/images/

我的 setup.py 如下:

from setuptools import setup
setup(name='hackertray',
      version='1.8',
      description='Hacker News app that sits in your System Tray',
      packages=['hackertray'],
      data_files=[('images', ['images/hacker-tray.png'])])

我的MANIFEST 文件是:

include images/hacker-tray.png

【问题讨论】:

    标签: python pip packaging easy-install package-managers


    【解决方案1】:

    不要将data_files 与相对路径一起使用。实际上,根本不要使用data_files,除非您确保目标路径是绝对路径,以跨平台的方式正确生成并插入硬编码值。

    改用package_data

    setup(
        # (...)
        package_data={
            "hackertray.data": [
                "hacker-tray.png",
            ],
        },
    )
    

    其中hackertray.data 是一个正确的python 包(即是一个包含名为__init__.py 的文件的目录),hacker-tray.png 就在__init__.py 旁边。

    它应该是这样的:

    .
    |-- hackertray
    |   |-- __init__.py
    |   `-- data
    |       |-- __init__.py
    |       `-- hacker-tray.png
    `-- setup.py
    

    您可以使用以下方法获取图像文件的完整路径:

    from pkg_resources import resource_filename
    print os.path.abspath(resource_filename('hackertray.data', 'hacker-tray.png'))
    

    希望对你有帮助。

    PS:Pythonpackage_data 中列出的文件打包方面存在错误。如果您使用 Python 2.7 之前的版本进行打包,请务必确保有一个清单文件。更多信息请看这里:https://groups.google.com/d/msg/python-virtualenv/v5KJ78LP9Mo/OiBqMcYVFYAJ

    【讨论】:

    • 一些澄清:1)当你说hackertray.data是一个正确的python包时,你是什么意思?我是否需要为数据创建另一个包,或者它仍然是原始包的一部分。 2)我按照你说的做了,结果发行版仍然没有把图像文件放在正确的位置。我已经上传了包here
    • 针对您的具体情况:cd hackertray-1.9; mkdir hackertray/data; touch hackertray/data/__init__.py; mv hackertray/hacker-tray.png hackertray/data; 并将hackertray.data 添加到 setup.py 中的packages 列表
    • 另外,我只使用find_packages() 而不是手动编写包列表。 from setuptools import find_packages 并将packages=find_packages() 传递给setup
    猜你喜欢
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多