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?

4
  • 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 Commented Aug 6, 2019 at 6:42
  • filename as string maybe? file='database.ini' Anyway, the error described in the questions suggests a different problem. Maybe this is helpful? Commented Aug 6, 2019 at 7: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. Commented Aug 6, 2019 at 7:26
  • 1
    try def function(file='./config/database.ini',section='sql'): Commented Aug 6, 2019 at 7:28

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.