Python使用pkgutil的extend_path查找子目录
这是我的文件夹结构
src
├── __init__.py
└── foo
├── __init__.py
└── bar
├── __init__.py
├── events
│ ├── __init__.py
│ ├── execute
│ │ ├── __init__.py
│ │ ├── helloworld.py
│ │ ├── run.py
└── settings.py
$ cat src/foo/__init__.py
输出...
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
在 src/foo/bar/events/execute/run.py
中,我想做这样的事情
from foo.bar.events.execute.helloworld import HelloWorld
我得到错误
No module named 'foo.bar'
这就是我运行我的应用程序的方式
我知道这不合适,但为了简洁起见,我简化了问题是有原因的
$ python src/foo/bar/events/execute/run
如何实现从 src/foo/bar/events/execute/run.py 导入这种方式?
from foo.bar.events.execute.helloworld import HelloWorld
This is my folder structure
src
├── __init__.py
└── foo
├── __init__.py
└── bar
├── __init__.py
├── events
│ ├── __init__.py
│ ├── execute
│ │ ├── __init__.py
│ │ ├── helloworld.py
│ │ ├── run.py
└── settings.py
$ cat src/foo/__init__.py
outputs...
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
in src/foo/bar/events/execute/run.py
, I want to do something like this
from foo.bar.events.execute.helloworld import HelloWorld
I get the error
No module named 'foo.bar'
This is how I'm running my app
I understand it's not proper, but there's a reason I just simplified the question for brevity
$ python src/foo/bar/events/execute/run
How do I achieve importing this way from src/foo/bar/events/execute/run.py
?
from foo.bar.events.execute.helloworld import HelloWorld
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论