-2

Say I have a folder named foo. Inside that folder is __init__.py, a folder called test, and another Python file called t1.py. Inside folder test is a Python file called bar.py, and in that file I am trying to do something like:

from foo import t1

And it gives me this error:

ModuleNotFoundError: No module named 'gmuwork'

Do I need to add something to environment variables or sys.path?

jwodder
  • 54,758
  • 12
  • 108
  • 124
rajivs
  • 1

1 Answers1

1

Absolute import

If you want to use

from foo import t1

Then yes, foo must be contained in sys.path. From the docs:

When importing the package, Python searches through the directories on sys.path looking for the package subdirectory.

In that case take a look at questions such as adding a file path to sys.path in python.

Relative import

Alternatively inside of bar.py you should be able to use

from ..foo import t1

as an intra-package reference.

Lastly: either way, you should put another empty __init__.py file inside of test to let Python know that folder is a subpackage.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235