tastyminerals
tastyminerals

Reputation: 6538

Why python setup.py installed module cannot find the installed resources?

I've correctly installed my python module into /usr/lib/python2.7/site-packages/mymod. However, when I attempt to run it with

python2 -m mymod

It runs only from /home/me/dev/mymod/mymod/ dir and fails if I do the same from any other directory.

IOError: [Errno 2] No such file or directory: 'mymod/data/icons/mymod.ico'

It has all the correct paths available to it:

/usr/lib/python2.7/site-packages/line_profiler-1.0-py2.7-linux-x86_64.egg
/usr/lib/python2.7/site-packages/textblob-0.11.1-py2.7.egg
/usr/lib/python2.7/site-packages/nltk-3.2-py2.7.egg
/usr/lib/python2.7/site-packages/pyCNN-0.0.0-py2.7-linux-x86_64.egg
/usr/lib/python27.zip
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/lib/python2.7/site-packages
/usr/lib/python2.7/site-packages/gst-0.10
/usr/lib/python2.7/site-packages/gtk-2.0
/usr/lib/python2.7/site-packages/wx-3.0-gtk2

Why can't it find mymod/data/icons/mymod.ico inside /usr/lib/python2.7/site-packages when it is there. I tried using different paths in mymod.py with mymod/data/icons/ and with data/icons but nothing helps.

$ file /usr/lib/python2.7/site-packages/mymod/data/icons/mymod.ico 
/usr/lib/python2.7/site-packages/mymod/data/icons/mymod.ico: MS Windows icon resource - 1 icon, 128x128

This issue has been plaguing all my python projects with setup.py and I think that there is something I clearly misunderstand about how python modules should be run.

Upvotes: 1

Views: 2461

Answers (1)

jbndlr
jbndlr

Reputation: 5210

I somehow think that your local reference is messing things up.

If you start python -m module from another directory, the relative file reference may still be interpreted to be relative to your working directory, not to the module which requires the file.

Try to reference your module-local file as follows and see whether it solves the problem:

from os import path
datadir = path.join(path.dirname(__file__), 'data')
icofile = path.join(datadir, 'icons', 'mymod.ico')

As proposed in this answer.

Upvotes: 1

Related Questions