-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?

2
  • Section 6.4 here should be useful Commented Aug 28, 2017 at 21:33
  • @BradSolomon: you're right, and I would say section 6.4.2 specifically, Intra-package References and relative import. Commented Aug 28, 2017 at 21:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.