15

I have a custom project package with structure like:

 package-dir/
     mypackage/
         __init__.py
         submodule1/
              __init__.py
              testmodule.py
         main.py
     requirements.txt
     setup.py

using cd package-dir followed by $pip install -e . or pip install . as suggested by python-packaging as long as I access the package from package-dir

For example :

 $cd project-dir
 $pip install .

at this point this works:

 $python -c 'import mypackage; import submodule1'

but This does not work

 $ cd some-other-dir
 $ python -c 'import mypackage; import submodule1'
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ImportError: No module named submodule1

How to install all the submodules?

also, if i check the package-dir/build/lib.linux-x86_64-2.7/mypackage dir, I only see the immediate files in mypackage/*.py and NO mypackage/submodule1

setup.py looks like:

from setuptools import setup
from pip.req import parse_requirements

reqs = parse_requirements('./requirements.txt', session=False)
install_requires = [str(ir.req) for ir in reqs]


def readme():
    with open('README.rst') as f:
        return f.read()

setup(name='mypackage',
      version='1.6.1',
      description='mypackage',
      long_description=readme(),
      classifiers=[

      ],
      keywords='',
      url='',
      author='',
      author_email='',
      license='Proprietary',
      packages=['mypackage'],
      package_dir={'mypackage': 'mypackage'},
      install_requires=install_requires,
      include_package_data=True,
      zip_safe=False,
      test_suite='nose.collector',
      tests_require=['nose'],
      entry_points={
          'console_scripts': ['mypackage=mypackage.run:run'],
      }
      )
4
  • does python -c 'from mypackage import submodule1' work in some-other-dir? Commented Jul 13, 2017 at 17:51
  • no only in same package-dir, update some more info at the end about build, if that helps Commented Jul 13, 2017 at 17:51
  • What's the content of setup.py? Specifically, do you have a line like packages=setuptools.find_packages()? Commented Jul 13, 2017 at 17:52
  • @NilsWerner update, and No I do not have that line .. will try that Commented Jul 13, 2017 at 17:56

1 Answer 1

28

setup.py is missing information about your package structure. You can enable auto-discovery by adding a line

setup(
    # ...
    packages=setuptools.find_packages(),
)

to it.

Sign up to request clarification or add additional context in comments.

2 Comments

I think find_packages() works because the submodule as shown has a __init__.py file. What happens when the submodule is actually the top-level directory with a setup.py file but no init.py, and the code is a directory below?
Please open a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.