Python:在安装Python自定义软件包中导入失败

发布于 2025-02-10 02:23:26 字数 619 浏览 1 评论 0原文

我有一个使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2025-02-17 02:23:26

您应该从.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 to package directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文