0

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.

5
  • 1
    did you try from eval import ngsa_two.PatientProblem (although assuming 'eval' is directory I strongly suggest change it to something more innocuous). Commented Jul 3, 2021 at 1:40
  • Thanks. That kind of works. I got a syntax error when using from eval import ngsa_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 ngsa_two works. ngsa_two.py only consists of PatientProblem. So this should be ok for this purpose. Like I said, this is a reference code so I haven't named any of these folders but will change Commented Jul 3, 2021 at 1:49
  • 1
    nsga_two or ngsa_two is the correct module name? Are you importing with wrong name which raise the error? Where is your main script located? Commented Jul 3, 2021 at 2:06
  • Sorry, typos throughout I just noticed. It should be nsga_two. The main script is located in a folder called MoodForecasting which contains other folders such as eval which have other .py files in them. Commented Jul 3, 2021 at 3:22
  • 2
    If from eval import ngsa_two works then what you want is from eval.ngsa_two import PatientProblem. You will want to read up on the syntax and semantics of the Python import statement. The suggestion in the first comment is not and has never been syntactically valid in Python. Commented Jul 3, 2021 at 5:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

The correct syntax would be:

from package.module import function

so:

from eval.nsga_two import PatientProblem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.