Importing Module Within Module Python3

Asked

Viewed 44 times

0

There are the following folders:

└── Programa/
    ├── main.py             <----- Inicio esse...
    ├── bin/
    |   └── config.py           <----- esse importa o db.py
    └── database/
        ├── __init__.py
        └── db.py

On the console shows that there is no module db, when I start the main, who matters the config and the config matters the db.

But when I care db straight from main make no mistake.

  • Could [Edit] and add to the question the relevant import codes in each file?

1 answer

0

If you just give an import db, it will scan the folders and subfolders from where it is running. Importing this way in config.py, it will look for the imported file (db.py) inside the folder it is in (bin/) and subfolders (in case there is none, inside the bin/ contains only config.py). Importing direct from main.py, it will scan both the files in your folder (which contains main.py) and its subfolders (which are the bin/ and the database/), and then it will find db.py, which is inside the database/

If you want to import db.py into config.py, at import time put all the way it is, starting at the root ('/').

Browser other questions tagged

You are not signed in. Login or sign up in order to post.