I have the following script inside my main file, and I would like to override the value set in the .env
file when running the script (e.g python -e MODE=train main.py
)
main.py
from dotenv import load_dotenv
load_dotenv()
import os
if __name__ == "__main__":
print(os.environ["MODE"])
.env
MODE=test
You can manage the command line arguments that your script receives
import os
import sys
from dotenv import load_dotenv
load_dotenv()
if __name__ == "__main__":
print(os.environ["MODE"])
param_mode = sys.argv[1]
value_mode = param_mode.split('=')[1]
os.environ["MODE"]=value_mode
print(os.environ["MODE"])
And you call would be something like this:
python main.py MODE=train
Explanation:
sys.argv[1] contains MODE=train string, so you split by '=' to get only the parameter value.