仅从__init __.py导入特定软件包

发布于 2025-01-26 06:10:14 字数 703 浏览 1 评论 0原文

我有一个通过__ Init __。py在名为python_utils的文件夹中定义的Python软件包,其中其中包含许多Python脚本。

 python_utils/
     __init__.py
     print_pretty.py
     script_1.py
     ...

为了方便起见,此__ Init __。py用来导入所有内容:

from .pretty_print import pretty_print
from .scritp_1 import func1
from .script_2 import func2
....

我遇到的问题是,某些python脚本取决于在运行时可能无法使用的软件包。因此,当我尝试从软件包中导入一个非常微不足道的函数(来自python_utils导入print_pretty)时,由于其他脚本所具有的依赖关系而失败。我还尝试直接从python_utils.print_pretty import inter_pretty 直接导入。我可以摆脱__ Init __。py来解决问题,但是在导入软件包时它可以节省很多键入。有什么方法我只能导入Pretty_print而无需从其他脚本中获得依赖关系?

I have a python package defined via __init__.py in a folder named python_utils that has many python scripts in it.

 python_utils/
     __init__.py
     print_pretty.py
     script_1.py
     ...

For convenience this __init__.py imports all of them using:

from .pretty_print import pretty_print
from .scritp_1 import func1
from .script_2 import func2
....

The problem I have is that some of the python scripts depend on packages that may not be available at run time. So when I try to import a very trivial function from the package (from python_utils import print_pretty), it fails because of the dependencies the other scripts have. I also tried to import directly with from python_utils.print_pretty import print_pretty same problem. I can get rid of the __init__.py to solve the problem, but it saves me a lot of typing when importing packages. Is there a way I can import only pretty_print without getting the dependencies from the other scripts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文