0

I am working on a project in python (First project). I am having a very hard time with importing between files.

My structure is as follow:

backend/
    ├── app/
    │   ├── services1/
    │   │   ├── __init__.py
    │   │   └── file1.py
    │   ├── services2/
    │   │   ├── __init__.py
    │   │   └── file2.py
    │   └── __init__.py
    └── ...

I am trying to import my function from file 1 to file 2. I am running file 2 in vscode. Both packages have init.py

The error:

ModuleNotFoundError: No module named 'app.services1.file1'

I’ve tried:

from app.services1.file1 import function

I am running the file2 from vscode and im in the backend directory.

fran
  • 3
  • 3
  • Could you please comment on your intended package structure? What are the intended top-level package(s), and how are they being put on the Python path? Do note that imports _do not navigate directories_. Imports work by searching the Python path. The only way to import your modules is by somehow putting them on the Python path. Typically that is done in one of two ways: by installing your project as a distribution package (nicer, and pretty simple these days), or by relying on the directory containing your package(s) being your CWD (ok for small projects, but precarious and bad for tooling) – Brian61354270 Aug 20 '23 at 21:40
  • 3
    Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time). The answers there cover some common confusions about scripts vs modules and directories vs packages. – Brian61354270 Aug 20 '23 at 21:42
  • Have you done anything that would add any of these directories to the default module search path? (usually this is done by defining the `PYTHONPATH` environment variable, or adding things to `sys.path`) – John Gordon Aug 20 '23 at 21:42
  • @JohnGordon do i need to add all my packages to the PYTHONPATH? I just did: import sys print(sys.path) and filepath1 is there but i dont see filepath2 Backend/app/services1/file1.py (in the sys i printed) Backend/app/services2/file2.py (not present) – fran Aug 21 '23 at 13:48
  • @Brian61354270 i will check that to see if it provides any info thank you. – fran Aug 21 '23 at 13:48
  • You don't have to add your packages to PYTHONPATH, but it is one way to import modules that are installed in a custom location (as you have here). If the Backend folder were part of your PYTHONPATH, then `import app.services1.file1` would definitely work. – John Gordon Aug 21 '23 at 14:18
  • Backend is though but the services2 is not is that normal? the services1 is part of a python path. '/Users/user1/code/username/reponame/backend' exists in my sys. @JohnGordon – fran Aug 21 '23 at 15:08
  • If the backend folder is part of your sys.path, then `from app.services1.file1 import function1` should have worked. If it didn't, then please post your **exact** file structure and the **exact** error traceback message. – John Gordon Aug 21 '23 at 15:25
  • @JohnGordon I have updated the question with the exact structure, the way im importing + the exact error when running the file. – fran Aug 22 '23 at 19:13
  • The question to ask yourself is this: if you run `file2.py` as an independent script, how would Python even know there is an `app` module? It isn't going to searching up the directory tree. You have to TELL it that. – Tim Roberts Aug 22 '23 at 19:15
  • I am able to run the files separately without a problem. It's just once I import, but I followed the structure like above and still it returns errors. @TimRoberts – fran Aug 22 '23 at 19:41
  • If the `backend` folder is on your `sys.path`, then `from app.services1.file1 import function` ought to have worked. Do the `backend` and `app` folders have `__init__.py` files? – John Gordon Aug 22 '23 at 21:21

1 Answers1

0

This is what we mean. In file2.py:

import os
import sys
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../..'))
from app.services1.file1 import function

Otherwise, Python has NO IDEA where to look to find "app".

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30