仅从__init __.py导入特定软件包
我有一个通过__ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论