1

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.

minerals
  • 6,090
  • 17
  • 62
  • 107
  • What is the chmod of `/usr/lib/python2.7/site-packages/mymod`? compare it with another lib. – loutre Mar 24 '16 at 09:39
  • `-rw-r--r--` it is read-only for normal user, but running it with sudo does not help. – minerals Mar 24 '16 at 09:42
  • If you take a look at the error message, your module seems to be found, but failed because of an ico file: `'mymod/data/icons/mymod.ico'`. Check the file exists. – loutre Mar 24 '16 at 09:43
  • All files exist, I am looking at them now. `setup.py` correctly installed all required resources for the app. – minerals Mar 24 '16 at 09:45

1 Answers1

1

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.

Community
  • 1
  • 1
jbndlr
  • 4,965
  • 2
  • 21
  • 31
  • Slap! you saved my life. It works. Still I don't understand why doesn't `setup.py` handle this automatically cause I could do the same thing with a bash script, create correct symlinks and bind everything together and it would be less work in the end. – minerals Mar 24 '16 at 09:58
  • I don't know whether or not this is intended behavior. It could also be one of python's *by design* aspects. If someone knows why an imported module should use relative references from the working directory rather than from its install dir, please share. – jbndlr Mar 24 '16 at 10:05
  • *the relative file reference may still be interpreted to be relative to your working directory* A relative path is **always** relative to your current working directory. How should a library module determine relative to which base directory a otherwise non specified relative path is supposed to be? – flaschbier Mar 24 '16 at 10:19
  • Relative to ``__file__`` for imported modules or their ``__init__.py``, for example. That's why I asked for language design aspects. – jbndlr Mar 24 '16 at 11:23