سؤال

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