Python开发包安装后无法访问子模块问题求助
Python开发包无法访问子模块问题
我之前多次成功安装自己的开发包,但现在遇到异常:每个模块都已添加__init__.py文件,通过python -m pip install -e .成功安装开发包(conda显示包处于<develop>状态),但无法通过mytestpackage.module1的方式访问子模块,执行时抛出如下错误:
AttributeError: module 'mytestpackage' has no attribute 'module1'
我可以正常执行import mytestpackage导入主包,但一旦尝试访问子模块就触发上述错误。
包目录结构
mytestpackage |- python setup.py |- mytestpackage __init__.py |- module1 __init__.py |- module2 __init__.py |- notebooks |- test |- somestuff |- README.md
setup.py内容
from setuptools import setup # Setting up setup( name="mytestpackage", version="0.0", description="some testing", author="me", author_email="me@myself.com", packages=['mytestpackage', 'mytestpackage.module1', 'mytestpackage.module2'], install_requires=[], )
已尝试的解决方法
- 替换手动指定的packages列表,使用
packages=setuptools.find_packages()和packages=setuptools.find_namespace_packages(),均未解决问题。 - 执行代码检查子模块存在性:
输出显示子模块确实存在:import pkgutil import mytestpackage for i in pkgutil.iter_modules(mytestpackage.__path__): print(i)ModuleInfo(module_finder=FileFinder('c:\\localdata\\repos\\mytestpackage\\python\\mytestpackage'), name='module1', ispkg=True) ModuleInfo(module_finder=FileFinder('c:\\localdata\\repos\\mytestpackage\\python\\mytestpackage'), name='module2', ispkg=True)
疑问
我以前只需添加__init__.py并运行setup.py就能正常访问子模块,是不是Python打包机制有变化?是否需要在主包的__init__.py中手动导入子模块,比如添加import mytestpackage.module1?
内容的提问来源于stack exchange,提问作者HCRuiz
相关产品推荐
相关产品推荐

