Adam
Adam

Reputation: 483

Adding new environment variable to dotenv file in an existing python project returning none

I have an existing Python 3.7 project. When adding a new variable to the .env, it is not loading.

Here is an example of my .env file.

# Existing
DB_HOST=localhost
DB_PORT=3307

# New variable
API_BASE_URL=https://testing.mysite.com.au/api/v1/
# Load api settings
api_base_url = os.getenv('API_BASE_URL') 

The database variables are working fine however, api_base_url returns None.

I don't believe it is a path/load_dotenv issue, as the existing variables are loading correctly.

Do I need to refresh/reload the .env file?

Upvotes: 1

Views: 3810

Answers (2)

gelonida
gelonida

Reputation: 5630

If you use any IDE like visual studio code / Pycharm or similiar you have probably to reload it after changing .env

Not knowing your exact environment and not knowing whether you use autoenv or any other tool it might also be a good choice to explicitly source .env as @it's-yer-boy-chet suggested.

If you're using autoenv, you just had to type cd . and it shoud prompt you whether you want tou source the modified version of .env

Upvotes: 0

it's-yer-boy-chet
it's-yer-boy-chet

Reputation: 2017

What environment management system are you using? I think the .env file is a pipenv thing, if so the .env file is only sourced when you call pipenv run ... or pipenv shell. So if you're working in a python you need exit the shell or run.

If you're on a linux machine you can also just source .env to assign the environment variables within the shell.

Upvotes: 1

Related Questions