[英]How to import python file from folder
I would like to import the config file. 我想导入配置文件。 it from sub-directory
从子目录
├── config
│ ├── config.py
│ ├── database.ini
│ └── log.py
├── main.py
config.py config.py
def function(file='database.ini',section='sql'):
return
database.ini database.ini
[sql]
host=1.1.1.1
user=admin
password=admin
database=sql
main.py 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" “在database.ini文件中找不到部分sql”
Now you see the structure of project. 现在您将看到项目的结构。 How to fix this?
如何解决这个问题?
The problem comes from the root directory in a python project set by default where your main is. 问题来自默认情况下您主程序所在的python项目中的根目录。 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
在您的情况下,您尝试访问
./database.ini
但是从您的根文件夹(其中main.py所在)访问该文件位于./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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.