Mar-25-2017, 03:23 AM
i want to establish a consistent scheme for config files for python program. for programs made for unix and unix-like platforms i want to use config file names that begin with a dot character so they are hidden like other config files. many programs have full or nearly-full programming power in the config fill and i want to have the same in the config files for programs in python. many script written various shell languages use the source command to run the config file which give it full programming power in the shell language. to do the same for python, the only way i see to do it is importing the config file as a module. the first problem is that the import command does not like names that begin with a dot. even the builtin.__import__() call fails at this. any next ideas to try?
file .tryconfig.py:
file tryprogram.py:
so i try it:
file .tryconfig.py:
1 2 3 |
# set some variables for a test config foo = 36 bar = 42 |
1 2 |
__import__ ( '.tryconfig' , globals ()) print (foo,bar) |
Output:lt1/forums /home/forums 4> ls -l .tryconfig.py;cat .tryconfig.py
-rw-r--r-- 1 forums forums 57 Mar 24 23:11 .tryconfig.py
# set some variables for a test config
foo = 36
bar = 42
lt1/forums /home/forums 5> ls -l tryprogram.py;cat tryprogram.py
-rw-r--r-- 1 forums forums 50 Mar 24 23:11 tryprogram.py
__import__('.tryconfig',globals())
print(foo,bar)
lt1/forums /home/forums 6> py3 tryprogram.py
Traceback (most recent call last):
File "tryprogram.py", line 1, in <module>
__import__('.tryconfig',globals())
ImportError: No module named '.tryconfig'
lt1/forums /home/forums 7> py2 tryprogram.py
Traceback (most recent call last):
File "tryprogram.py", line 1, in <module>
__import__('.tryconfig',globals())
ValueError: Empty module name
lt1/forums /home/forums 8>
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.