0

I have a file named .env in my code directory .env has different configs

Eg : username = "xyz"
     password = "abcd"

In my code directory I have a folder called scripts

Inside scripts I have a script named "control.py" which needs to read the config from .env

How can I achieve this?

The structure is something as below

/var/www/html/code/.env
/var/www/html/code/scripts/control.py

.env has all the config stuffs and control.py needs to read these config.

Coder
  • 107
  • 4
  • 15
  • maybe keep config in different format like JSON, YAML. They have modules to read it. – furas Sep 10 '19 at 03:48
  • I cant do that, the config is PHP file and needs to stay as it is. – Coder Sep 10 '19 at 03:50
  • 1
    Possible duplicate of [Reading in environment variables from an environment file](https://stackoverflow.com/questions/40216311/reading-in-environment-variables-from-an-environment-file) – otus666 Sep 10 '19 at 04:02
  • if file looks like python's code then you could load it - `text = open(filename).read()` - and execute it - `exec(text)`. But if it doesn't look like Python's code then you will have to manually find data in file. – furas Sep 10 '19 at 04:03

1 Answers1

0

It would be better if you use decouple module for this purpose. You can do pip install python-decouple.

ANISH TIWARI
  • 111
  • 1
  • 10