Python package setup.py issue: can only use 'from' imports to access modules, and cannot access them from the package namespace

Viewed 22

I have a package which I have written and installed onto a virtual environment in editable mode. I can only import from this package only when the modules and items within those modules are imported using the 'from' syntax.

In a python file outside the package, I can import specific modules from the package using from package import module and import specific functions/objects from these modules via from package.module import x in external scripts/python interpreter. However, when I try to import the whole module, I find that the package has no accessible modules; i.e. if I were to write:

import package
x = package.module.x

Then I would receive the error:AttributeError: module 'package' has no attribute 'module'. Intriguingly , if I use a from import and then attempt the same command again, the error does not occur and the attribute, object or function 'x' imports properly.

I believe that the problem should have something to do with how the package is found in the setup.py, but I don't know enough about python packaging to understand what is going on

I have this module installed in editable mode through pip on my anaconda virtual environment, and it uses the standard cookiecutter python format. There are no sub-packages, and the init.py contains only basic bibliographic information ('_name_ ' and '_email_' and so on).

here is my setup:

"""The setup script."""

from setuptools import setup, find_packages

with open('README.rst') as readme_file:
    readme = readme_file.read()

with open('HISTORY.rst') as history_file:
    history = history_file.read()

requirements = ['Click>=7.0', ]

test_requirements = ['pytest>=3', ]

setup(
    author="Alexander Pasha",
    author_email={email},
    python_requires='>=3.9',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.9',
    ],
    description={description},
    install_requires=requirements,
    license="MIT license",
    long_description=readme + '\n\n' + history,
    include_package_data=True,
    keywords={name},
    name={name},
    packages=find_packages(include=['package', 'package.*']),
    test_suite='tests',
    tests_require=test_requirements,
    url={GitHub},
    version='0.1.0',
    zip_safe=False,
)

for reference, I'm using python 3.9.12

0 Answers
Related