0

I have the following directory:

└── myproject
    ├── moduleA
        │   ├── __init__.py
        │   ├── users.py
    ├── moduleB
        │   ├── __init__.py
        │   ├── test_in_B.py
    ├── test.py

I can easily run test.py whose content is the following:

from moduleA import users
print(users.name)

However, I cannot run test_in_B.py whose content is the same as the above and get ModuleNotFoundError: No module named 'moduleA' error.

Contents for replication:

moduleA (__init__.py)

from moduleB import *

moduleB (__init__.py)

from moduleA import *

users

name = 'user1'

Question

How can I run test_in_B.py keeping the same structure?

Saeed
  • 598
  • 10
  • 19
  • 1
    Does this answer your question? [Sibling package imports](https://stackoverflow.com/questions/6323860/sibling-package-imports) – Dr. V Dec 12 '22 at 18:50

1 Answers1

0

You can only import modules located in a direct subdirectory of the directory where the .py file is.

There is a workaround described in see Importing files from different folder. But I would generally recommend avoiding this code structure to make your code easier to read and maintain.

James King
  • 66
  • 3