0

I would like to import the config file. it from sub-directory

├── config
│   ├── config.py
│   ├── database.ini
│   └── log.py
├── main.py

config.py

def function(file='database.ini',section='sql'):
   return 

database.ini

[sql] 
host=1.1.1.1
user=admin
password=admin
database=sql

main.py

from config.config import function

def Run(): 
    Test = function()    

if __name__=="__main__":
   Run()

The error warning look like
"Section sql not found in the database.ini file"

Now you see the structure of project. How to fix this?

DilbertFan
  • 113
  • 1
  • 1
  • 9
zzob
  • 993
  • 3
  • 9
  • 19
  • This cannot be what you are executing `def function(file=database.ini,section='sql'):`would yield `NameError: name 'database' is not defined`. There must be some code trying to open database.ini – MofX Aug 06 '19 at 06:42
  • filename as string maybe? `file='database.ini'` Anyway, the error described in the questions suggests a different problem. Maybe [this](https://stackoverflow.com/questions/27012337/python-config-parser-cant-find-section) is helpful? – FObersteiner Aug 06 '19 at 07:13
  • This code it's work when i move main.py into the config/ folder. So i guess the problem it's from path/directory. – zzob Aug 06 '19 at 07:26
  • 1
    try `def function(file='./config/database.ini',section='sql'):` – Doc Aug 06 '19 at 07:28

1 Answers1

1

The problem comes from the root directory in a python project set by default where your main is. In your case your try to acces ./database.ini but from your root folder (where main.py is) this file is at ./config/database.ini
To fix your code change this line

def function(file='database.ini',section='sql'):
    pass

by this line

def function(file='./config/database.ini',section='sql'):
    pass
Sagar Gupta
  • 1,352
  • 1
  • 12
  • 26
IQbrod
  • 2,060
  • 1
  • 6
  • 28