Monday, November 15, 2021

[SOLVED] How to package a python module that imports another module within that package

Issue

I am creating a python 3.7.2 package that I am then installing in different location, inside a virtualenv to use in an application. My package has multiple modules with dependencies (imports) between them. I can't figure out how to get the package to load correctly so that I can use the dependent modules in the package.

Package directory

root
\- foo # my package
   \- __init__.py # empty or with from .helper_class import HelperClass
   \- helper_class.py
   \- my_class.py # imports helper_class
   setup.py

Application directory

app
\- main.py # from foo.my_class import MyClass
\- venv

When my_class doesn't import helper_class, I can package, install and run main.py just fine. When I import helper_class in my_class, I get ModuleNotFoundError.

$ python main.py
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from foo.my_class import MyClass
  File "/Users/XXXXXX/tmp/foo/my_class.py", line 1, in <module>
    from helper_class import HelperClass
ModuleNotFoundError: No module named 'helper_class'

I've tried an empty __init__.py, and also adding from .helper_class import HelperClass to __init__.py. I've added .s to the references all over the place, but no love.

There must be something extremely obvious that I'm missing.

app/main.py


o = MyClass()
print(o.get_att())

foo/my_class.py

from helper_class import HelperClass

class MyClass():
    def __init__(self):
        self.att = 123
    def get_att(self):
        return self.att

foo/helper_class.py

class HelperClass():
    def __init__(self):
        pass

Setup.py below (taken from https://github.com/navdeep-G/setup.py)

    name=NAME,
    version=about['__version__'],
    description=DESCRIPTION,
    long_description=long_description,
    long_description_content_type='text/markdown',
    author=AUTHOR,
    author_email=EMAIL,
    python_requires=REQUIRES_PYTHON,
    url=URL,
    packages=['foo', ],
    # packages=find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]),
    # If your package is a single module, use this instead of 'packages':
    # py_modules=['mypackage'],

    # entry_points={
    #     'console_scripts': ['mycli=mymodule:cli'],
    # },
    install_requires=REQUIRED,
    extras_require=EXTRAS,
    include_package_data=True,
    license='MIT',
    classifiers=[
        # Trove classifiers
        # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy'
    ],
    # $ setup.py publish support.
    # cmdclass={
    #     'upload': UploadCommand,
    # },
)

Solution

Please try to import it as below ...

from foo.helper_class import HelperClass


Answered By - MK Patel