0

I know this question has been asked a lot, and I've read tens of blogs about the issue too, but there simply must be something I'm missing.

My understanding is that using the sys.path method is not pythonic, so I'm trying to do this in the best way.

I have an app which has the following file structure:

application
  ├── db
  │   └── db_setup.py
  │   └── read_db.py
  │   └── write_db.py
  │   └── database.db
  │
  ├── server
  |   └── server1.py
  |   └── server1.py
  |
  ├── web
      └── #contains all the files for a React web app

One of the servers is responsible for handling database requests so it needs access to the read and write python files.

I've tried each of the following in server1.py: from db import read_db import db from db import read_db from db.read_db import *

I've tried including an __init__.py file in the db and server folders (in both, and in one but not the other for both) as well as doing relative imports using dot syntax (e.g. from .db import read_db).

I will always get one of the following errors:

attempted relative import with no known parent package or no module named

The only thing I can think of is that it's not possible to within an application like this define modules or packages that can import into others in the same application... but that just doesn't sound right.

Fonty
  • 239
  • 2
  • 11

2 Answers2

0

So possibly the answer was that I was running server1.py (via uvicorn) from the console while in the servers folder, so there appeared to be no way to access the parent (even via dot syntax). If instead was in the app folder and ran uvicorn servers.server1:app I was able to import db and call anything inside that. I guess it makes sense, but it does also seem strange that it behaves that way... and that nothing in the blog posts seem to point to this as an "obvious" gottcha.

Fonty
  • 239
  • 2
  • 11
0

Where is your entry point? You should have an entrypoint on the root folder, it should look like this:

application
  |
  ├── main.py
  |
  ├── db
  │   └── db_setup.py
  │   └── read_db.py
  │   └── write_db.py
  │   └── database.db
  │   └── __init__.py
  |
  ├── server
  |   └── server1.py
  |   └── server1.py
  │   └── __init__.py
  |
  ├── web
      └── #contains all the files for a React web app

If you start the application from you main.py you should have all modules available to all other modules.

For example you have this method in your db/write_db.py:

def addToDatabase():
   print("adding entry to db")

Then you can access the method from server/server1.py like so:

from db import write_db

def server():
   write_db.addToDatabase()

Don't forget to have an init.py in every subfolder otherwise it won't work.

Source: Import paths - the right way?

bramdc
  • 580
  • 1
  • 5
  • 21
  • You were correct. The entry point wasn't correct. No `main.py` or anything, just running the server python stuff _in the folder it lives in_ which was the problem. Running it from the root application folder solved the issue. – Fonty Jul 05 '21 at 21:22