ModuleNotFoundError when it actually is in the path

Viewed 96

I've tried adding it directly to path through control panel ('c:\projects\parser'), and also appending it before I try my import. It works with the childs folder, not the parents.

.
└── parser
    └── tests
        ├── __init__.py
        └── utils
            ├── __init__.py
            └── rebuild_parsers.py

This is what I've tried.

C:\>python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('c:\\projects\\parser')
>>> from tests.utils.rebuild_parsers import build_lexer_parser
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'tests.utils'

>>> sys.path.append('c:\\projects\\parser\\tests')
>>> from utils.rebuild_parsers import build_lexer_parser

I don't really understand why is it not working. Both folders (tests and utils) have an __init__.py file

Of course if I run it from the parser folder, it works, I just want to understand why it doesn't get it as a module.

C:\Projects\parser>python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from tests.utils.rebuild_parsers import build_lexer_parser
>>>
1 Answers

Tried the same tree structure you gave. It seems to have worked for me.

Using Mac and python3.8.7.

Here's the tree:

% tree parser
parser
└── tests
    ├── __init.py__
    └── utils
        ├── __init.py__
        └── rebuild_parsers.py

This is what my rebuild_parsers.py contains:


# rebuild_parsers.py
def exmaple():
        print("Hello")

Here's the code I wrote in the terminal:

>>> import os, sys
>>> sys.path.append(os.path.join(os.getcwd(), "parser"))
>>> from tests.utils.rebuild_parsers import exmaple
>>> example()

It seems to have worked. Can you check it out.

Related