0

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?

lr_optim
  • 299
  • 1
  • 10
  • 30
  • how you have tried to import ? – Abhishek Kumar Sep 08 '22 at 13:11
  • Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Maurice Meyer Sep 08 '22 at 13:26
  • @AbhishekKumar I've tried importing it like `from bar import function_1`. The IDE finds it and I can access the functions but when I run it up with docker, the module is not found. I also tried using dots in front of the import, but I had to use `from .......bar import function_1` to be able to access the top level. The error I then get is: `ImportError: attempted relative import beyond top-level package`. – lr_optim Sep 09 '22 at 05:37

0 Answers0