-1

I desesperatly searching for 1 liner load config package in Python 3... In python 2, there is :

https://pypi.python.org/pypi/config/0.3.7
from config import Config; cfg = Config(file('D://config.py', 'r'))

There is https://pypi.python.org/pypi/loadconfig but it does not seem loading file...

Main goal is to install this package in all the python envs and load one big config file for all envs.

Use case:

You have a single piece of python, you want to run on several hardware/OS and python envs (conda,...) without changing the code...

Method 1:
    1) Define a generic config files for all Envs needed.
    2) At RunTime, determine the environnment (linux, username, ) and 
       define the absolute path root repository directory with config file.
    3) Single piece of code, running in different envs without changing the code, the envs...
tensor
  • 3,088
  • 8
  • 37
  • 71
  • That line doesn't close the file properly. It shouldn't be used in any version of Python. Why does it need to be one line? – jpmc26 Sep 19 '17 at 06:23
  • this is copy paste in many piece of code. updates with file in python 2.7 – tensor Sep 19 '17 at 06:26
  • You need a `with` statement or a `try`/`catch` statement to close the file properly. See [here](https://stackoverflow.com/q/112970/1394393) for `file` vs. `open`. Also, shoving all the code into one line doesn't make it easier to copy/paste. If you're copy/pasting it thousands of places or something, it would be worth just putting in a shared module. – jpmc26 Sep 19 '17 at 06:28
  • No, because it helps defining the root CWD directory (defined manually since dependant on hardware disk directory). With a module, you cannot edit easily. – tensor Sep 19 '17 at 06:30
  • Current working directory should only be used for one thing: default location for input and output files. It should also be determined by the caller; the program should not change it (at least not without changing it back immediately). In memory (variables), you're better off using absolute paths everywhere. (Call `abspath` if you need to.) If you have companion files with your script, then use the script's file location instead. (Get it from `__file__` or `inspect`.) On top of that, anything that differs between scripts can easily be parameterized in a function you write in your own module. – jpmc26 Sep 19 '17 at 06:40
  • There is some kind of mis-understanding... Question is how do you determine the location of some files if we don't have the root folder ? (/home/username/ProjectRoot/) – tensor Sep 19 '17 at 07:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154754/discussion-between-tensor-and-jpmc26). – tensor Sep 19 '17 at 07:05
  • There is absolutery no reference to local path , Working directory is relative to the the Absolute path since different environnments are involved. – tensor Sep 19 '17 at 07:53

1 Answers1

0

From similar functionnalities than config

https://pypi.python.org/pypi/config/0.3.7

Security is not guaranted config.py should not contains harmful python code.

    from attrdict import AttrDict; f= open('USER_ROOT/config.py'); cfg = AttrDict(eval(f.read())); f.close()

    cfg.key1
    cfg.mykey2
    cfg.mykey4
tensor
  • 3,088
  • 8
  • 37
  • 71