Importing Module Inside Python3 Module

0

There are the following folders:

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

In the console it shows that there is no module db , and I start main , which imports config and config imports db .

But when I import the db direct from main it does not give error.

    
asked by anonymous 06.07.2018 / 08:57

1 answer

0

If you only give an import db, it will scan the folders and subfolders from where it is running. Importing from this form in config.py, it will look for the imported file (db.py) inside the folder where it is (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 of your folder (containing main.py) and its subfolders (which are the bin / and the database /), and then it will find db.py , which is within database /

If you want to import db.py into config.py, at import time put the whole path it is in, beginning at the root ('/').

    
06.07.2018 / 12:44