Search code examples
pythonimportrpmdistutilssetup.py

Package import creates a module, submodules still importable


I've been working on a python package and now I would like to turn it into a small RPM distribution. Package includes a few modules, one of which is executable. I can create the RPM-package with python setup.py bdist_rpm and install it on a fedora box with rpm.

At this point have the desired command myscript and it works like a charm. But when i try to import the package in ipython, I run into something strange. I can do the following

from myscript import sdf
import myscript.mol2

Both work flawlessly, but

import myscript
myscript.sdf

throws

AttributeError: 'module' object has no attribute 'sdf'

I've been working with this for a while now to no avail. There's plenty of questions of import problems, but I haven't found an answer for this one yet.

What I should change to make it work?

The current folder structure is:

myscript/  #project root
  setup.py
  src/
    myscript/
      __init__.py
      functions.py
      sdf.py
      mol2.py
      runner.py
  bin/
    myscript  #symbolic link to src/myscript/runner.py

setup.py is:

from distutils.core import setup
setup(name = 'myscript',
      version = '0.75',
      author ='me',
      requires = ['numpy'], 
      packages = ['myscript'],
      package_dir = {'myscript':'src/myscript'},
      scripts = ['bin/myscript']
      )

and __init__.py is:

__all__ = ['functions','sdf','mol2','runner']

Solution

  • This is normal behavior. If you want submodules to be imported then you must import them in the module.

    # myscript/__init__.py
    
    from . import sdf