How to import from a root directory in Python?

Viewed 32

I have a FastAPI app with multiple modules. I need to import a module from the root folder and I don't know how to do it.

Here's my folder structure:

package/
       app/
          app/
             __init__.py
             api/
                __init__.py
                api_v1/
                   __init__.py
                   endpoints/
                      __init__.py
                      foo.py
   
package_2/
   __init__.py
   bar.py

In foo.py, I'd have to import a function from bar.py but can't make it work. I'm getting import errors like ModuleNotFoundError: No module named 'package_2'. How could I make this import work?

0 Answers
Related