문제

I have a script containing, among other things, the following structure. Is the passage in "script_2.py" a good way to import variables from a external file regardless if the location of script_2.py and config.py is changed (assuming they will always "stay together")?

...Or am I missing something and making it more difficult than it should be?

# scrip_1.py
.
.
.
ThirdPartyBlackBoxProgramCall('somePath\\script_2.py')
.
.
.

.

# script_2.py
import os
import sys

folder_path = os.path.dirname(__file__)
sys.path.append(folder_path)
from config import A, B, C
.
.
.

.

# config.py
A = '111'
B = '222'
C = '333'
도움이 되었습니까?

해결책

Think of it this way - if your package is installed by root into the system directory - there's no way for the user to override the defaults in your config file.

Instead of using a python file as the config, use an actual config module like ConfigParser and then do something like site.py does where it looks in the system directories, and then the user's home directory .yourpackage.rc

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top