2

I have a package with the following structure:

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

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?, Relative imports in Python 3

Daniel
  • 471
  • 3
  • 8
  • 18

2 Answers2

3

Append your script/module path with sys module then import your sub modules.

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

Hope this will solve your problem.

Edit:

Modified your main_trainer file

#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 *
  • So I should be adding this to each and every module? – Daniel Jun 13 '20 at 20:59
  • 1
    The place where you're importing specific libraries. `from model.snn import *` and `from model.splitter import *` – Vanarajan Natarajan Jun 13 '20 at 21:01
  • so sys.path.append('/path/../model/') in my main_trainer.py script, sys.path.append('/path/../model/main_trainer/') or sys.path.append('/path/../model/snn/') and sys.path.append('/path/../model/splitter/') – Daniel Jun 13 '20 at 21:03
  • If I add sys.path.append('path/to/my/projects/'), the folder which contains the model folder, which in turn contains the modules, I get Traceback (most recent call last): File "main_trainer.py", line 18, in from .snn import * ModuleNotFoundError: No module named '__main__.snn'; '__main__' is not a package – Daniel Jun 13 '20 at 21:10
  • you might be using that `mail.snn`, I can't say without seeing your code, but I did one more edit where your can point till the model folder and them do `import snn` and `splitter` separately – Vanarajan Natarajan Jun 13 '20 at 21:15
  • I have edited the post to clarify that I am using if __name__ == '__main__' at the end of the function in main_trainer, sorry for not mentioning it before – Daniel Jun 13 '20 at 21:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215909/discussion-between-vanarajan-natarajan-and-daniel). – Vanarajan Natarajan Jun 13 '20 at 21:17
0

According to the results of my own operation, this problem was solved exactly:

  1. Install modAL-python in Anaconda Prompt (pip install modAL-python)
  2. Enter python in Anaconda Prompt ((base) C:\Users\TEST>python)
  3. Execute the statement in the python environment of the second step: import modAL
  4. Execution statement: help(modAL)
  5. Restart the application that is using modAL.