Why ModuleNotFound when importing a module in a same directory?

Eiffelbear

Question

When I try to activate main.py on linux bash with the command as follows,

python3 main.py

The error message looking as below keeps appearing, and I cannot figure out why!!

File "main.py", line 1, in <module>
    import folder_beta.util_one
File "folder_beta/util_one.py", line 1, in <module>
    ModuleNotFoundError: No module named 'util_two'

Questions in more detail

The folder tree looks like as below:

folder_alpha
├── main.py
└── folder_beta
      ├── __init__.py (empty)
      ├── util_one.py
      └── util_two.py

main.py

import folder_beta.util_one
import folder_beta.util_two
....

util_one.py

import util_two
...

When I executed the 'util_one.py' alone, it works perfectly fine but when I executed the main.py, the error keeps appearing.

Can anyone tell me how to fix this problem, please?

wim

That is an implicit relative import, it would have worked in Python 2 but it's no longer allowed in Python 3. From PEP 8:

Implicit relative imports should never be used and have been removed in Python 3.

In util_one.py module, change it to:

from folder_beta import util_two

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error when importing module from a different folder in the same parent directory

From Dev

Python not importing module from same directory

From Dev

Difference between python 2.7 and 3.3+ when importing in __init__.py and module from same directory

From Dev

When importing random number from module in two different files, why are they the same?

From Dev

ModuleNotFound when trying to install Python Extension Module (Cython) inside the same package specified in setup.py

From Dev

Why do I have to put a dot before module when import it from the module within same directory?

From Dev

Python Errors when importing module from child directory

From Dev

ModuleNotFoundError: No module named when importing from the sub directory

From Dev

'Module not found' when importing a library service in the same workspace

From Java

Importing from builtin library when module with same name exists

From Dev

TypeScript is compiling into this failing JS file when importing a module. Why?

From Dev

Why is importing module not working when running the code for the second time

From Dev

Error when importing a module

From Dev

Reactjs Importing from the same directory

From Dev

Importing classes defined in the same module

From Dev

importing module with same name as file

From Dev

Importing module named same as package

From Dev

Python kafka module when used with pyspark causes 'ModuleNotFound' error?

From Java

Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package

From Dev

ModuleNotFound: No module named 'app'

From Dev

ImportError with module in same directory

From Dev

Require module in same directory

From Dev

Module not found error when file exists in same directory

From Dev

Module not found error when importing

From Dev

Why is this Typescript module importing and then reexporting another module?

From Dev

Importing datetime raises ModuleNotFound 'math' in Python script when executed by command line

From Dev

Python importing a module from a parallel directory

From Dev

Importing module with "current-directory" imports

From Dev

Importing a haskell module from the parent directory

Related Related

  1. 1

    Error when importing module from a different folder in the same parent directory

  2. 2

    Python not importing module from same directory

  3. 3

    Difference between python 2.7 and 3.3+ when importing in __init__.py and module from same directory

  4. 4

    When importing random number from module in two different files, why are they the same?

  5. 5

    ModuleNotFound when trying to install Python Extension Module (Cython) inside the same package specified in setup.py

  6. 6

    Why do I have to put a dot before module when import it from the module within same directory?

  7. 7

    Python Errors when importing module from child directory

  8. 8

    ModuleNotFoundError: No module named when importing from the sub directory

  9. 9

    'Module not found' when importing a library service in the same workspace

  10. 10

    Importing from builtin library when module with same name exists

  11. 11

    TypeScript is compiling into this failing JS file when importing a module. Why?

  12. 12

    Why is importing module not working when running the code for the second time

  13. 13

    Error when importing a module

  14. 14

    Reactjs Importing from the same directory

  15. 15

    Importing classes defined in the same module

  16. 16

    importing module with same name as file

  17. 17

    Importing module named same as package

  18. 18

    Python kafka module when used with pyspark causes 'ModuleNotFound' error?

  19. 19

    Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package

  20. 20

    ModuleNotFound: No module named 'app'

  21. 21

    ImportError with module in same directory

  22. 22

    Require module in same directory

  23. 23

    Module not found error when file exists in same directory

  24. 24

    Module not found error when importing

  25. 25

    Why is this Typescript module importing and then reexporting another module?

  26. 26

    Importing datetime raises ModuleNotFound 'math' in Python script when executed by command line

  27. 27

    Python importing a module from a parallel directory

  28. 28

    Importing module with "current-directory" imports

  29. 29

    Importing a haskell module from the parent directory

HotTag

Archive