Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x ConfigParser.NoSectionError_Python 3.x_Jupyter Notebook_Configparser - Fatal编程技术网

Python 3.x ConfigParser.NoSectionError

Python 3.x ConfigParser.NoSectionError,python-3.x,jupyter-notebook,configparser,Python 3.x,Jupyter Notebook,Configparser,我对python和笔记本电脑比较陌生,但是我在config.txt文件方面遇到了问题。我认为问题在于笔记本与我导入的模块位于一个单独的文件中(config.txt文件与导入的模块位于同一目录中)。当我在笔记本中运行模块时,我得到了“NoSectionError:No section:”错误。但是,当我将代码复制并粘贴到笔记本中时,代码运行正常。 工作目录有问题吗 from src.data import psql_operations db_connection = psql_operation

我对python和笔记本电脑比较陌生,但是我在config.txt文件方面遇到了问题。我认为问题在于笔记本与我导入的模块位于一个单独的文件中(config.txt文件与导入的模块位于同一目录中)。当我在笔记本中运行模块时,我得到了“NoSectionError:No section:”错误。但是,当我将代码复制并粘贴到笔记本中时,代码运行正常。 工作目录有问题吗

from src.data import psql_operations
db_connection = psql_operations.PostgresConnection()

class PostgresConnection:
    def __init__(self):
        config = configparser.ConfigParser()
        config.read(r....'/src/data/config.txt') 
        self.host = config.get('psql database', 'host')
        self.db_name = config.get('psql database', 'db_name')
        self.user = config.get('psql database', 'user')
        self.password = config.get('psql database', 'password')
        self.port = config.get('psql database', 'port')
vs


经过大约一周的努力,我问了这个问题,一个小时后找到了解决办法。需要更改配置文件的路径。需要将以下内容添加到加载的模块中

导入操作系统

config.read(os.path.join(os.path.abspath(os.path.dirname(文件),“…/src/data”,“config.txt”))

下面的链接涉及相同的问题

经过大约一周的努力,我问了这个问题,一小时后找到了解决方案。需要更改配置文件的路径。需要将以下内容添加到加载的模块中

导入操作系统

config.read(os.path.join(os.path.abspath(os.path.dirname(文件),“…/src/data”,“config.txt”))

下面的链接涉及相同的问题

config = configparser.ConfigParser()
config.read(r.....'/src/data/config.txt')
host = config.get('psql database', 'host')
db_name = config.get('psql database', 'db_name')
user = config.get('psql database', 'user')
password = config.get('psql database', 'password')
port = config.get('psql database', 'port')