5

I know to set an environment variable in python is to use os.environ['API_USER'] but where this variable is saved, I supposed this environment variable is saved in .env file but it wasn't.

on the console to retrieve all the environment variables use command: os.environ but don't know where are saved. need your help, Thanks!

4
  • Environment variables are specific to each process, on linux they can be accesses in /proc. Why do you need to know where they are saved? Commented Nov 20, 2019 at 22:30
  • because I am working on a project with Django, and need to save environment variable in the .env file, so after typing os.environ['API_USER'] in the shell I supposed the .env will be updated with the API_USER but it wasn't , so i wonder where it is saved
    – Flupper
    Commented Nov 20, 2019 at 22:40
  • If API_USER is a constant why do you not already have it as an environment variable? Commented Nov 20, 2019 at 22:42
  • yes it is constant, I was just debugging because when I try to retrieve it in the settings.py os.environ.get('API_USER') also it is in the .env file !
    – Flupper
    Commented Nov 20, 2019 at 22:46

2 Answers 2

5

Environment variables live in the memory, not on the disk. People usually save environment variables in files only for not having to do the same exporting of them by hand repetitively.

Also note that, environment variables are properties of operating system processes, and the process specific ones are passed on to all of the subprocesses of that process.

So when you run os.environ, it shows the environment variables and their values belonging to the python process (the executable that is being executed).

5
  • so the meaning of what you said if the webserver process of Django is running and I opened the Python shell and type os.environ all the Environment variables for that process will be listed?
    – Flupper
    Commented Nov 20, 2019 at 22:43
  • @Flupper When you open the Python shell that's a new python process that is running, which has no relation to the Django webserver running in any way. And it's environment is totally different from Django's (unless you have exported the same set of environment varaiables while they start, and even then some environment variables would be different).
    – heemayl
    Commented Nov 20, 2019 at 22:46
  • when I listed the environment variables of python shell process why some variables relating to my Django project appears like Tz: timezone DJANGO_SETTINGS_MODULE PS1 . ?!
    – Flupper
    Commented Nov 20, 2019 at 22:51
  • @Flupper Because you've export-ed them from the shell and then from the same shell you've open python. (See the subprocess rule on my answer).
    – heemayl
    Commented Nov 20, 2019 at 22:53
  • I got it, Thank you for your clarification
    – Flupper
    Commented Nov 20, 2019 at 23:01
2

There is a way to use a .env file to store environment variables, using the python-dotenv package. Documentation can be found here

A simple example would be to create a .env file with the contents:

API_USER=username

Then, in your code you can use:

from dotenv import load_dotenv
load_dotenv()

Note that without specifying a path to the .env file it assumes it is in the same directory. For a more detailed example on specifying a path, see the documentation.

You can then access the environment variable using os.getenv('API_USER')

1
  • 1
    thanks for sharing with me this package, I gonna use it
    – Flupper
    Commented Nov 20, 2019 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.