导入后无法找到模块
Unable to find module after import
我是 运行nin Python 3.6.3,我在尝试通过 pip 安装的子目录中有以下模块。
/g_plotter
setup.py
/g_plotter
__init__.py
g_plotter.py
Gparser.py
setup.py
from setuptools import setup
setup(
name='g_plotter',
packages=['g_plotter'],
include_package_data=True,
install_requires=[
'flask',
],
)
我在我的容器中安装了那个模块形式 Docker:
RUN pip3 install ./g_plotter
然后在我的应用代码中:
import g_plotter
print(dir(g_plotter))
输出
server_1 | ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
所以改用这个导入:
from g_plotter import g_plotter
结果
server_1 | Traceback (most recent call last):
server_1 | File "./g_server.py", line 21, in <module>
server_1 | from g_plotter import g_plotter
server_1 | File "/usr/local/lib/python3.7/site-packages/g_plotter/g_plotter.py", line 7, in <module>
server_1 | import Gparser
server_1 | ModuleNotFoundError: No module named 'Gparser'
当我 运行 自己的子模块(它是一个烧瓶应用程序)时,它可以工作。
必须在python中使用绝对导入 3、import Gparser
不再被允许。您可以将其更改为:
from . import Gparser
from g_plotter import Gparser
为了让你更清楚,我来描述一下它们是什么意思。
import Gparser
Gparser = load_module()
sys.modules['Gparser'] = Gparser
from g_plotter import Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
from . import Gparser
package = find_package(__name__) # 'g_plotter'
Gparser_name = package + 'Gparser' # g_plotter.Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
现在你明白了,如果你直接运行g_plotter,其实__name__
就是__main__
,所以python找不到包它。只有在其他模块中导入这个子模块,from . import something
才可以工作。
我是 运行nin Python 3.6.3,我在尝试通过 pip 安装的子目录中有以下模块。
/g_plotter
setup.py
/g_plotter
__init__.py
g_plotter.py
Gparser.py
setup.py
from setuptools import setup
setup(
name='g_plotter',
packages=['g_plotter'],
include_package_data=True,
install_requires=[
'flask',
],
)
我在我的容器中安装了那个模块形式 Docker:
RUN pip3 install ./g_plotter
然后在我的应用代码中:
import g_plotter
print(dir(g_plotter))
输出
server_1 | ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
所以改用这个导入:
from g_plotter import g_plotter
结果
server_1 | Traceback (most recent call last):
server_1 | File "./g_server.py", line 21, in <module>
server_1 | from g_plotter import g_plotter
server_1 | File "/usr/local/lib/python3.7/site-packages/g_plotter/g_plotter.py", line 7, in <module>
server_1 | import Gparser
server_1 | ModuleNotFoundError: No module named 'Gparser'
当我 运行 自己的子模块(它是一个烧瓶应用程序)时,它可以工作。
必须在python中使用绝对导入 3、import Gparser
不再被允许。您可以将其更改为:
from . import Gparser
from g_plotter import Gparser
为了让你更清楚,我来描述一下它们是什么意思。
import Gparser
Gparser = load_module()
sys.modules['Gparser'] = Gparser
from g_plotter import Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
from . import Gparser
package = find_package(__name__) # 'g_plotter'
Gparser_name = package + 'Gparser' # g_plotter.Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
现在你明白了,如果你直接运行g_plotter,其实__name__
就是__main__
,所以python找不到包它。只有在其他模块中导入这个子模块,from . import something
才可以工作。