3

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.

5
  • "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. Commented Dec 2, 2021 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. Commented Dec 2, 2021 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. Commented Dec 2, 2021 at 10:08
  • 1
    Check stackoverflow.com/questions/716011/… or stackoverflow.com/questions/17657686/… out Commented Dec 2, 2021 at 10:10
  • or you can just load env file every time. I think it is better than messing around with OS stuff. Commented Dec 2, 2021 at 10:11

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
How can I set the environment variables then so thay I can use them from othe processes or from the system itself?
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 stackoverflow.com/questions/70216898/… 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.
This explains what a system is and how we set environment variables there in case my comment is unclear linuxize.com/post/…
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

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.