I got an error that Python couldn't read settings.ini

Content of this article

Sometimes the code that worked on one PC didn't work on a different PC. The cause seemed to be that the relative path was not read properly, so I will describe the solution as a reminder. I don't know why the solution works at this point, so I'll update the article when I understand it.

table of contents

  1. Background
  2. Arrangement of events
  3. Solution
  4. Why the solution works

1. Background

When the code I wrote for creating a simple application started to exceed 1,000 lines, I started to feel the limit to writing code in a one-page python file, so processing I decided to divide it and manage it by dividing it into multiple python files. For the initial values such as login information, I decided to create a `settings.ini``` file and read it from `settings.pyusing configparser```. I also started using github the other day to manage updates. Since the first time overlapped and many things I did not understand came out, I will keep a record as a reminder.

2. Arrangement of events

When I cloned the Repository created on the main PC from git to the sub PC, there were some things that didn't work for some reason. What I didn't understand was that I couldn't read `settings.ini``` which was referenced by the relative path from `settings.py```.

On the main PC, the code below worked fine.

settings.py



import configparser

conf = configparser.ConfigParser()
conf.read('./settings.ini')

#Profile path
PROFILE_PATH = conf['driver']['PROFILE_PATH']

# saleceforce
Saleceforce_ID = conf['saleceforce']['Saleceforce_ID']
Saleceforce_PASS = conf['saleceforce']['Saleceforce_PASS']
Saleceforce_ADDRESS = conf['saleceforce']['Saleceforce_ADDRESS']

However, when I run this on a sub PC, the following error occurs. Apparently it turned out that './settings.ini' was not loaded properly.

raise KeyError(key)
KeyError: 'driver'

3. Solution

If you rewrite this as follows, it will work.

settings.py


import configparser
import os

conf = configparser.ConfigParser()
path = os.path.join(os.path.dirname(__file__), 'settings.ini')
conf.read(path, 'UTF-8')

#Profile path
PROFILE_PATH = conf['driver']['PROFILE_PATH']

# saleceforce
Saleceforce_ID = conf['saleceforce']['Saleceforce_ID']
Saleceforce_PASS = conf['saleceforce']['Saleceforce_PASS']
Saleceforce_ADDRESS = conf['saleceforce']['Saleceforce_ADDRESS']

If it is a relative path, it can not be read well, so I read the directory of the executable file with ```os.path.dirname (__ file__) `` `and specified it directly and it worked. This may be better when running code in a different environment.

4. Why the solution works

I don't know why. I will update it when I understand it.

Recommended Posts

I got an error that Python couldn't read settings.ini
I got an error in vim and zsh in Python 3.7 series
I got an error when saving with OpenCV
About the point that python 3.x got stuck due to an error due to caching_sha2_password
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]
I get an Import Error in Python Beautiful Soup
I got an error when using Tensorboard with Pytorch
5 reasons I got into Python
When I installed python on macOS and used it, I got an error when I put an https connection
I got an AttributeError when mocking the open method in python
I got an error when I ran composer global require laravel / installer
I sent an SMS with Python
I got an error when I ran meteor add accounts-password and got hooked
I got an error when trying to install Xgboost and its solution
I got an SSL related error with pip install, so I solved it
I got an unfamiliar error in Django: TypeError: resolve () got an unexpected keyword argument'strict'
I get an error with import pandas.
I tried sending an email with python.
When I get an error with PyInstaller
What I got from Python Boot Camp
[Python] I want to know the variables in the function when an error occurs!
I got an error when trying to run Hello World in Go language
I want to send Gmail with Python, but I can't because of an error
I wrote an IPython Notebook-like Tkinter wrapper [Python]
I made an extenum package that extends an enum
A memo that I wrote a quicksort in Python
Create an app that guesses students with python
Building an environment that uses Python in Eclipse
Let's write FizzBuzz with an error: Python Version
I got stuck installing Anaconda 4.3.0 which became Python 3.6
I wrote an IPython Notebook-like Gtk wrapper [Python]
I get an error with all yum commands
I tried sending an email with SendGrid + Python
I got an error when pip install pandas on Mac, so I dealt with it
I get an error when I try to raise Python to 3 series using pyenv on Catalina