Pycharm Error: ModuleNotFoundError: No module named 'pymoo.algorithms'; 'pymoo' is not a package

4.1k views Asked by At

I've tried to run the example code from pymoo for NSGA2 in PyCharm.

from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.factory import get_problem
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter

problem = get_problem("zdt1")

algorithm = NSGA2(pop_size=100)

res = minimize(problem,
               algorithm,
               ('n_gen', 200),
               seed=1,
               verbose=False)
plot = Scatter()
plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7)
plot.add(res.F, facecolor="none", edgecolor="red")
plot.show()

It gives me always this Error:

/Users/myname/opt/miniconda3/envs/namename/bin/python /Users/myname/PycharmProjects/name/namename/pymoo.py
Traceback (most recent call last):
  File "/Users/myname/PycharmProjects/name/namename/pymoo.py", line 1, in <module>
    from pymoo.algorithms.moo.nsga2 import NSGA2
  File "/Users/mynae/PycharmProjects/name/namename/pymoo.py", line 1, in <module>
    from pymoo.algorithms.moo.nsga2 import NSGA2
ModuleNotFoundError: No module named 'pymoo.algorithms'; 'pymoo' is not a package

I have already downloaded pymoo under the preferences and when I try to download pymoo in the terminal again it says that all packages are already installed.

Can anyone help?

1

There are 1 answers

0
Lomtrur On BEST ANSWER

Rename your file, for example to pymoo_test.py (or something else entirely) and it should work.

Your stack trace tells me that your file is called pymoo.py:

File "/Users/mynae/PycharmProjects/name/namename/pymoo.py", line 1, in <module>

The file name is what is causing your problem. What's happening is that the imports are being tried to be imported from your pymoo.py file due to the name and you trying to import from pymoo.<...> in the same file.