views:

30

answers:

1

I wrote a standalone script depends on a few modified modules. the directory structure looks like this:

client
  setup.py
  tsclient
    __init__.py
    tsup
    utils.py
    mutagen
      __init__.py
      blah.py
      blah.py
      ...
    colorama
      __init__.py
      blah.py
      blah.py
      ...

currently, if I just symlink the usup script to my ~/bin directory, I can invoke the script directly and it works with no problems (everything imports properly with no problems).

Now I want to make a setup.py script so I can distribute it. I can't figure out how to do it. Here is what I have now:

setup(
  name='tsclient',
  version='1.0',
  scripts=['tsclient/tsup'],
  packages=['tsclient', 'tsclient.mutagen', 'tsclient.colorama'],
)

The problem is that I can't just do import mutagen in the tsup script because it's now tsclient.mutagen. If I change the import to say from tsclient import mutagen I get this error (from mutagen's __init__.py file):

ImportError: No module named mutagen._util

I don't think the best solution is to go through mutagen and change every single instance of "mutagen" and change it to "tsclient.mutagen". Is this my only option?

+1  A: 

Unfortunately you do need to edit mutagen to make this work.

Fortunately Python 2.5 and later have syntax to support exactly what you're doing.

See http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports .

Suppose mutagen currently says,

from mutagen import _util

If you change it to say

from . import _util

then it will continue to work as a top-level package; and if needed you can move the whole thing into a subpackage and it will still work.

(However, if you are using setuptools, you can instead add a install_requires= argument in setup.py to tell setuptools that your package requires mutagen to be installed. Then your package could just import mutagen directly.)

Jason Orendorff