AcidCatfish
AcidCatfish

Reputation: 324

Python Module not found error - not getting fixed

So I am pretty new with Python. I've been working on running with a code similar to one I hope to build myself (so a reference code). Aside from some bugs I need to work out with invalid syntax, all seems to work except for one issue with one particular .py file I have.

My structure is this:

MoodForecasting -> eval -> nsga_two.py

I do have _init_.py in eval folder though, so I'm not sure why this block of code isn't working.

I am trying to load one particular fucntion from it, so the structure should look like this

from nsga_two import PatientProblem

Unfortunately, I keep getting the error ModuleNotFoundError: No module named 'nsga_two'.

I checked nsga_two.py itself and found that it couldn't load inspyred. I went in and was able to fix this. So, nsga_two.py runs fine on its own. However, I cannot import it into the main script I will be working with.

Some extra details: I am working with the IDE Spyder with Python Custom Version 3.7.9.

I'm not sure if it is an issue with Spyder or just how I am loading in my working directory. (Most of my coding experience is in MatLab and R so having an IDE similar to RStudio and MatLab is the reason I chose to work in Spyder)

Edit: I got a syntax error when using from eval import nsga_two.PatientProblem. Python didn't like the period. So, I instead tried it with no period. I got the error cannot import name 'nsga_twoPatientProblem' from 'eval' (C:\Users\name\Desktop\MoodForecasting-master\MoodForecasting-master\eval\__init__.py). I don't know why. But doing from eval import nsga_two works. nsga_two.py only consists of PatientProblem. This solve should be ok for this purpose. I'm just not sure why this could be happening.

Upvotes: 0

Views: 125

Answers (2)

Alexis Andrade
Alexis Andrade

Reputation: 76

The correct syntax would be:

from package.module import function

so:

from eval.nsga_two import PatientProblem

Upvotes: 1

adamkwm
adamkwm

Reputation: 1173

Suppose your structure is like:

MoodForecasting-master/
    main.py
    eval/
        __init__.py
        nsga_two.py

When you run the main script to import something, the directory of that script is added to the module search path sys.path, .../MoodForecasting-master/ in this case. from nsga_two import PatientProblem raised ModuleNotFoundError because nsga_two.py is not in that directory.

As Iguananaut said, from eval import nsga_two.PatientProblem in the first comment has never been a valid statement. The valid ways of doing so are:

  1. import by from eval import nsga_two and use as nsga_two.PatientProblem().
  2. import by from eval.nsga_two import PatientProblem and use as PatientProblem() directly.

Module search starts from .../MoodForecasting-master/, first option go to .../MoodForecasting-master/eval/ to find nsga_two.py, second option go to .../MoodForecasting-master/eval/nsga.py to find attribute named PatientProblem.

Upvotes: 1

Related Questions