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.
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.
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'
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.
I don't know why. I will update it when I understand it.
Recommended Posts