1

I wish to read an .env file with python and expose the environment variables so they can be accessed by other scripts in the same environment. I have this script :

from dotenv import load_dotenv
import os

load_dotenv("common.env")
print(os.environ["CONFIG_FOLDER_NAME"])

When I run this script it prints the correct value.

However when I try to read the variables from a different file they don't seem to exist. Even if I do echo $CONFIG_FOLDER_NAME it return empty.

KZiovas
  • 3,491
  • 3
  • 26
  • 47
  • "when I try to read the variables from a different file ": how do you that? What does that file look like? And `echo` isn't a Python command/function, so I'm not sure why you go from Python to shell. – 9769953 Dec 02 '21 at 10:07
  • 4
    This isn't creating environment variables, it merely populates Python's internal `os.environ` dictionary with values from a file *as if they had been set using environment variables*. It does nothing outside of this Python process. – deceze Dec 02 '21 at 10:08
  • 3
    Nothing you do in the script can change the environment variables in the calling shell. The most you could do would be to make it print the shell commands that are necessary to set those environment variables in the shell, and then in the shell you would have to capture that output and run those commands. – alani Dec 02 '21 at 10:08
  • 1
    Check https://stackoverflow.com/questions/716011/why-cant-environmental-variables-set-in-python-persist or https://stackoverflow.com/questions/17657686/is-it-possible-to-set-an-environment-variable-from-python-permanently out – Yoshikage Kira Dec 02 '21 at 10:10
  • or you can just load env file every time. I think it is better than messing around with OS stuff. – Yoshikage Kira Dec 02 '21 at 10:11

1 Answers1

2

Ok so based on the comment above, to help future users who might not be aware of this, you cannot set environment variables outside the scope of the current process with Python.

You can make python aware of some variables and change env variables for the scope of a process and its child processes. But you can not set values for env in the system itself or other processes (that are not children of the current process). For example if I set a env variable called HOST_URL it wont be actually accessible in the system environment.

I found three ways to actually set the variables by:

  1. Running a bash script to set the env variable values
  2. Use VSCode launch.json for setting the variables either with env or envFile
  3. Define them through Docker file or docker-compose.yml if you are containerizing your app

Note: If there are other options to address this please comment and I ll add them. I want this to be a helpfull post for new developers like myself. Shaming and non-helpful comments never helped anyone or improved anything. This is, or should be, a learning and knowledge exchange platform and it should be open to all programming questions Stack Overflow Isn’t Very Welcoming. It’s Time for That to Change

KZiovas
  • 3,491
  • 3
  • 26
  • 47
  • 1
    `you cannot set environment variables from Python`.... That is incorrect. You can set env vars using Python, or just about any other language. What you can't do is modify the environment of an arbitrary process (e.g., the parent of your Python program) without its cooperation. – Kurtis Rader Dec 03 '21 at 15:37
  • How can I set the environment variables then so thay I can use them from othe processes or from the system itself? – KZiovas Dec 03 '21 at 22:44
  • I don't know what you mean "from the system itself". In any case this is a FAQ that gets asked several times each week. See https://stackoverflow.com/questions/70216898/how-to-export-environment-variable-permanently-using-ruby for one recent example. One option is to use your Python program to set its environment then use something like `os.execl` to replace the python process with a shell. That shell will inherit the python environment. – Kurtis Rader Dec 04 '21 at 20:46
  • This explains what a system is and how we set environment variables there in case my comment is unclear https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/ – KZiovas Dec 04 '21 at 22:07
  • It is good that the question is asked every week, that means people are trying to learn stuff about programming and stack overflow is the place to ask questions – KZiovas Dec 04 '21 at 22:16