1

I'm relatively new to python and working with notebooks, but I am having problems with the config.txt file. I think the problem is from the notebook being in a separate file as the module I import (the config.txt file is in the same directory as the imported module). When I run the module in the notebook, I get the 'NoSectionError: No section:' error. However, when I copy and paste the code into the notebook, the code runs just fine. Is there a problem with the working directory?

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 = 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')
Justin Davis
  • 203
  • 2
  • 6

1 Answers1

0

After about a week of trying to figure this out, I asked the question to find the solution an hour later. The path to the config file needed to be changed. The following needed to be added to the loaded module

import os

config.read(os.path.join(os.path.abspath(os.path.dirname(file)), '.../src/data', 'config.txt'))

The following link touches on the same problem configparser not working in Python 3.4, NoSectionError but works fine in PyCharm

Justin Davis
  • 203
  • 2
  • 6