2

I'm confused about the significance of source .env and why Python is not accessing those variables although those variables are accessible from bash.

When I manual export FOO=foo, Python sees the env, but not when I source .env

$ cat .env 
ENV=development
$ echo $ENV

$ echo $FOO

$ source .env 
$ export FOO=foo
$ echo $ENV
development
$ echo $FOO
foo
$ python3
Python 3.7.7 (default, Mar 10 2020, 15:43:27)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.getenv("ENV"))
None
>>> print(os.getenv("FOO"))
foo
7
  • 2
    ENV is not exported, so.. ? Why would you expect python to see it? Commented Jul 19, 2020 at 13:42
  • I guess I though if bash was able to see $ENV that meant is was exported. What does "exported" mean to you? I suppose I don't understand that part. Commented Jul 19, 2020 at 13:43
  • 1
    What does "exported" mean to you? It means that a variable has set the export attribute. And you set that with export call. Nowhere you do export ENV.. Commented Jul 19, 2020 at 13:44
  • So how would I easily go about exporting all key-value vars in an .env file? Thanks btw Commented Jul 19, 2020 at 13:45
  • Easily? set -a Commented Jul 19, 2020 at 13:46

2 Answers 2

2

I'm confused about the significance of source .env

source just works like "include" in other languages, like in C it includes the content of other file. source just parses the text from other file as-if it would be typed in.

Why Python is not accessing those variables although those variables are accessible from bash.

Because the variable is not exported, when the execution environment for python process is created, the value of that variable is not included in that environment.

So how would I easily go about exporting all key-value vars in an .env file?

  • Add export to each variable in .env file, or
  • Use set -a to export all variables and then source the file, or
  • Parse the .env file to extract variable names and set the export attribute on each variable with export.
Sign up to request clarification or add additional context in comments.

Comments

1

As KamilCuk said, source does not necessary "export". Variables must be exported, not just sources, so in my case I would just have export ENV=development in the .env file and source that.

Notice in this case .env is different on line 2.

$ cat .env 
export ENV=development
$ echo $ENV

$ echo $FOO

$ source .env 
$ export FOO=foo
$ echo $ENV
development
$ echo $FOO
foo
$ python3
Python 3.7.7 (default, Mar 10 2020, 15:43:27) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.getenv("ENV"))
development
>>> print(os.getenv("FOO"))
foo

And to add to this, when I did export FOO=foo, this both assigns and exports, but in fact if you were to just do the following, you would create a bash var without export.

$ FOO=foo
$ echo $FOO
foo

In this case it's assigned, but not exported. Python needs it to be exported.

Comments

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.