Python:在安装Python自定义软件包中导入失败
我有一个使用setup.py创建的python软件包,然后安装在虚拟环境中。目录结构是:
root
|
+-- setup.py
|
+-- package
|
+-- __init__.py
|
+-- main.py
|
+-- common
|
+-- __init__.py
|
+-- helper.py
我有一种在helper.py.py
中存在的方法,并在main.py.py
中导入by common.helper income.helper incort.helper import func
。创建此轮的轮子并将其安装并用作全局模块后,我将获得common
的导入错误。但是将导入语句从package.common.helper导入func 工作。是否有一种方法可以将common
定义为软件包
的本地子模块,以便即使以软件包的方式安装了common
作为本地subsodule。
I have a python package created using setup.py and then installed in a virtual environment. The directory structure is:
root
|
+-- setup.py
|
+-- package
|
+-- __init__.py
|
+-- main.py
|
+-- common
|
+-- __init__.py
|
+-- helper.py
I have a method that exist in helper.py
and importing in main.py
by from common.helper import func
. After creating a wheel of this and installing and using that as a global module I am getting an import error for common
. But changing the import statement to from package.common.helper import func
works. Is there a way to define the common
as a local submodule to package
so that even if is installed as a package it uses common
as a local submodule.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该从.common.helper导入func 使用
。您的代码失败了,因为它无法在全球范围内识别为包裹,因为它是
package
目录的子弹。You should use
from .common.helper import func
. You code is failing because it cannot recognise common as a package globally, since it is subpackage topackage
directory.