Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ModuleNotFoundError in Python but the module is there

I have a directory called step_2_my_hand, and inside it I have 3 python packages; app.py, general_num_and_suit_list.py and __init__.py.

Inside app.py, I have the following:

from generate_num_and_suit_list import generate_num_list_from_my_hand

def run_num_list_suit_list():
    num_list, suit_list = generate_num_list_from_my_hand()

    # num list is an integer at this point, and suit list is made up of strings
    return num_list, suit_list

And when I run this module directly it works.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In my top level directory, in the same level as the step_2_my_hand directory, I have another app.py, which contains the following:

from all_the_steps_with_coordinates.step_2_my_hand.app import run_num_list_suit_list

Now, when I run this, I get the error:

...
from generate_num_and_suit_list import generate_num_list_from_my_hand
ModuleNotFoundError: No module named 'generate_num_and_suit_list'

But generate_num_and_suit_list.py is a module in the same directory. I checked and there is no spelling mistake.

>Solution :

When you run it in different directory (top-level directory) it will search for generate_num_and_suit_list in that (top-level) directory. Try replacing it with:

from .generate_num_and_suit_list import generate_num_list_from_my_hand

...

Note the . that means "this module".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading