简体   繁体   English

无法从模块导入模块

[英]Cannot import Module from Module

I am trying to import a module that imports a module.我正在尝试导入一个导入模块的模块。 But that doesnt work.但这不起作用。 This is my folder hierarchy:这是我的文件夹层次结构:

/Project/
-test.py
     /recognizer/
     -__init__.py
     -tools.py
     -feature_extraction.py

I am importing tools.py inside of feature_extraction.py .我在feature_extraction.py中导入tools.py If I run the feature_extraction.py it all works like it should.如果我运行feature_extraction.py ,一切都会正常运行。 Now I want to import the feature_extraction.py from the test.py , but that doesnt work.现在我想从test.py导入feature_extraction.py ,但这不起作用。 I get this error ModuleNotFoundError: No module named 'tools' .我收到此错误ModuleNotFoundError: No module named 'tools'

Inside the test.py I am importing like this from recognizer import feature_extraction that works like it should I think.test.py中,我from recognizer import feature_extraction中像这样导入,它的工作方式与我想的一样。 Inside of feature_extraction.py I am importing just like this import tools .feature_extraction.py里面我就像这个import tools一样导入。 This should work I think, but it doesnt.我认为这应该有效,但它没有。

You should do this way.你应该这样做。

from . import tools

If you import tools from feature_extraction.py file.如果您从feature_extraction.py文件中import tools it will lookup main directory which test.py lays.它将查找test.py的主目录。

try to import in feature_extraction tools like this: import recognizer.tools尝试像这样导入 feature_extraction 工具:import Recognizer.tools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM