pip installing data files to the wrong place

2019-02-22 13:09发布

The source for the package is here

I'm installing the package from the index via:

easy_install hackertray
pip install hackertray

easy_install installs images/hacker-tray.png to the following folder:

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

While, pip installs it to:

/usr/local/images/

My setup.py is as follows:

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'])])

My MANIFEST file is:

include images/hacker-tray.png

1条回答
戒情不戒烟
2楼-- · 2019-02-22 13:26

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

查看更多
登录 后发表回答