简体   繁体   English

ModuleNotFoundError:没有名为“模型”的模块

[英]ModuleNotFoundError: No module named 'model'

I have a package with the following structure:我有一个具有以下结构的 package:

model\
    __init__.py (from model.main_trainer import *, etc.)
    main_trainer.py
    snn.py
    splitter.py

The main_trainer.py script takes at least three arguments as inputs: main_trainer.py 脚本至少需要三个 arguments 作为输入:

#main_trainer.py

import numpy as np # Linear algebra
import pandas as pd # Data wrangling
import re # Regular expressions

import matplotlib


# Avoid plotting graphs
matplotlib.use('Agg')

# Custom dependencies
from model.snn import *
from model.splitter import *

def main_trainer(dataset_name, model_dict = None, train_dict = None,
                 how = 'k-fold cross-validation', save = True):
    etc.

if __name__ == '__main__':
    dataset_name, model_dict, train_dict, how = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
    main_trainer(dataset_name, model_dict, train_dict, how)

However, if I run in the terminal the following:但是,如果我在终端中运行以下命令:

python main_trainer.py dataset_name model_dict train_dict 'k-fold cross-validation'

I get the following error:我收到以下错误:

Traceback (most recent call last):
  File "main_trainer.py", line 17, in <module>
    from model.snn import *
ModuleNotFoundError: No module named 'model'

On the other hand, if I use the relative path as such:另一方面,如果我这样使用相对路径:

# Custom dependencies
from .snn import *
from .splitter import *

I get this error:我收到此错误:

Traceback (most recent call last):
  File "main_trainer.py", line 17, in <module>
    from .snn import *
ModuleNotFoundError: No module named '__main__.snn'; '__main__' is not a package

I have also tried running it as:我也尝试将其运行为:

python -m main_trainer ...

and then I get this error:然后我得到这个错误:

Traceback (most recent call last):
  File "/home/kdqm927/miniconda3/envs/siamese/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/kdqm927/miniconda3/envs/siamese/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/projects/cc/kdqm927/PythonNotebooks/model/main_trainer.py", line 17, in <module>
    from .snn import *
ImportError: attempted relative import with no known parent package

I have checked these posts to no avail: ModuleNotFoundError: What does it mean __main__ is not a package?我检查了这些帖子无济于事: ModuleNotFoundError: What is mean __main__ is not a package? , Relative imports in Python 3 , Python 中的相对导入 3

Append your script/module path with sys module then import your sub modules. Append 带有sys模块的脚本/模块路径,然后导入您的子模块。

sys.path.append('/path/to/your/model/modules/')

Hope this will solve your problem.希望这能解决您的问题。

Edit:编辑:

Modified your main_trainer file修改了你的main_trainer文件

#main_trainer.py

import numpy as np # Linear algebra
import pandas as pd # Data wrangling
import re # Regular expressions
import sys
import matplotlib


# Avoid plotting graphs
matplotlib.use('Agg')

# Custom dependencies
sys.path.append('/projects/cc/kdqm927/PythonNotebooks/model/') #folder which contains model, snn etc.,
from snn import *
from splitter import *

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

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