pip installing data files to the wrong place

橙三吉。 提交于 2019-12-01 17:45:55

Don't use data_files with relative paths. Actually, don't use data_files at all, unless you make sure the target paths are absolute ones properly generated in a cross-platform way insted of hard coded values.

Use package_data instead:

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

where hackertray.data is a proper python package (i.e. is a directory that contains a file named __init__.py) and hacker-tray.png is right next to __init__.py.

Here's how it should look:

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

You can get the full path to the image file using:

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

I hope that helps.

PS: Python<2.7 seems to have a bug regarding packaging of the files listed in package_data. Always make sure to have a manifest file if you're using something older than Python 2.7 for packaging. See here for more info: https://groups.google.com/d/msg/python-virtualenv/v5KJ78LP9Mo/OiBqMcYVFYAJ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!