Comments (5)
I had an issue that looked similar, but it had nothing to do with python-dotenv, instead it was caused by default behavior of VSCode that I didn't understand. I'm leaving this note for anyone else finding their way here, because I think it's highly likely this was @mmsaki's underlying issue.
It seems the built-in Python extension of VSCode has a feature that "sources" your .env
file into any new VSCode terminal in a Python project (source). This is controlled by the python.envFile
setting that is by default set to ${workspaceFolder}/.env
. This sourcing happens in an idiosyncratic way that matches the reproduction case from the issue, see more below.
If you aren't aware of this default behavior (and of your terminal's actual environment), like I was, it can lead to unexpected results with regards to python-dotenv usage. For example, it could explain the "caching" behavior that @mmsaki observed, which was also my issue. Here's how to reproduce it:
- Have an
.env
file in a Python project with some env vars. - Open a new VSCode terminal.
- Change, add or remove variables in your
.env
file. - Call a Python program that uses python-dotenv's
load_dotenv()
in the same terminal (withoutoverride=True
) - Observe: the initial environment variables & their values values from step (1), as sourced by VSCode, will have overridden the actual state of your
.env
. You're working with old state created before the program executed, hence "caching".
Additionally, I've tested the initial reproduction case from the issue, which also matches in behavior:
- Add
VARIABLE="xxxxxxx" # this is a comment
to your.env
- Have the
python.envFile
workspace setting set to the default value - Open a new VSCode terminal
- Run
echo $VARIABLE
- Output:
"xxxxxxx" # this is a comment
This is idiosyncratic, since it is different from how zsh, bash and python-dotenv would load this env variable.
To prevent VSCode from loading .env
this way, set { "python.envFile": "" }
in your workspace settings.
from python-dotenv.
Also the dotenv package is not watching for real time changes in the .env
file. Why is this happening? This does not look good. Is the file being cached? It looks like dotenv is called on time and does not load the .env
again no matter how many times you rerun your code. Wooow!
from python-dotenv.
I cannot reproduce your issue with the provided input.
There is however a behavior in this package that you might have encountered with slightly different input.
If your .env is (note the '#' glued to the first word):
VARIABLE=xxxxxxx# this is a comment
Then indeed the '#' does not behave as an inline comment indicator. This behavior is kind of consistent with bash.
If you source .env
in bash you get bash: this: command not found
If your .env is:
VARIABLE=xxxxxxx#this_is_NOT_a_comment
and you source it in bash, you will see the VARIABLE is set to "xxxxxxx#this_is_NOT_a_comment".
So you need a space before the '#' to make it a comment in bash.
The other issue you observe might be the fact, that load_dotenv
does not override existing variables by default. You can control this behavior with the named argument override
. For example load_dotenv(override=True)
.
from python-dotenv.
Thanks @Bajron for help here. @mmsaki I hope this resolve your issues. Please re-open the issue if it doesn't, please be as detailed as possible.
from python-dotenv.
@th0rgall yes, this was exactly the behavior I was hoping to highlight when I opened this issue. Thanks for sharing your discovery and uncovering the unexpected behaviors which are coming from the built-in Python extension in VSCode and not python-dotenv. Thank you for your effort. I am sure this will help a lot of people coming across this issue again.
from python-dotenv.
Related Issues (20)
- Two underscores w/ load_dotenv() == None HOT 3
- cli.py functions names collide with built-ins
- easily allow sourcing .env-file in bash HOT 2
- dotenv module not found in python 3.11 version HOT 2
- The .env file opens writable by default HOT 2
- How to get openai api_key from the local environment [if api_key is already set in the environment ] HOT 1
- OSS-Fuzz Integration HOT 3
- Consider MIT license HOT 1
- Consider allowing directory to search for in `find_dotenv` HOT 1
- Cannot import dotenv module when python file runs by java. HOT 1
- Connection string in .env-file not read or parsed properly HOT 17
- AssertionError when attempting to locate dotenv file HOT 4
- cannot use pipx installed python-dotenv on Mac HOT 7
- Support for dumping to a .env file?
- [Question] Any significant cost to calling load_dotenv() multiple times?
- Too many open files
- Configure `dotenv_values`'s search space
- How to disable python-dotenv HOT 2
- Trying to get in touch regarding a security issue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-dotenv.