Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If using a .env file with Docker, the Flask CLI still tells you to install python-dotenv #3872

Closed
nickjj opened this issue Jan 14, 2021 · 7 comments

Comments

@nickjj
Copy link
Contributor

nickjj commented Jan 14, 2021

If you have a .env file and are using Docker you don't need to install python-dotenv.

But every time you run any flask CLI command, Flask will output * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. to stdout before any other output.

Perhaps this tip could be removed all together? IMO if you're using env files you likely already know to use python-dotenv and if you're using Docker you have access to your environment variables through other means.

Expected Behavior

No tip output to be present.

Actual Behavior

It produces this:

image

Environment

  • Python version: 3.9.1
  • Flask version: 1.1.2
  • Werkzeug version: 1.0.1
@SouravPal1995
Copy link

I was facing a similar issue when using "pipenv". "pipenv" automatically detects .env files and does not need "python-dotenv" package", but flask still wants you to install it..

@davidism
Copy link
Member

davidism commented Jan 18, 2021

If you're using another tool that loads dotenv files before flask executes, you can set FLASK_SKIP_DOTENV=true to stop it from checking for (or loading) the files. Since the other tool is loading it, you could put that in the dotenv file.

The warning was added in #2668, and it helped me catch an incomplete setup more than a few times, so I think it should stay for now.

@nickjj
Copy link
Contributor Author

nickjj commented Jan 18, 2021

you can set FLASK_SKIP_DOTENV=true to stop it from checking for (or loading) the files

Is there a case to be made to have it disabled / skipped by default?

Especially since most deployment platforms allow you to insert environment variables through their CLI (such as Heroku). Combine that with other application runtime environments (systemd, Docker, Kubernetes, ECS, etc.) having their own mechanisms for loading environment variables without python-dotenv.

It's nice that the skip env variable exists but the current implementation means you need to set it in every application you deploy. If it's the norm, shouldn't it be the default? This way in production it defaults to disabled because nearly everyone would be using Docker or systemd or some other hosted solution. Then in development you can choose to overwrite it as needed.

@davidism
Copy link
Member

Docker Compose and pipenv, while convenient tools, are not nearly the norm. Also, if you're deploying you should never be running the development server, at which point you'd never see this message anyway because only the flask run and app.run() commands try to load dotenv files. If the message were skipped by default, it would entirely defeat the purpose of the message.

@nickjj
Copy link
Contributor Author

nickjj commented Jan 18, 2021

Also, if you're deploying you should never be running the development server, at which point you'd never see this message anyway because only the flask run and app.run() commands try to load dotenv files

Then I believe there's a bug in how the tip is being output because right now if you run any flask command it will produce that tip by default even if you don't use flask run.

Personally I don't even use flask run in development, I always use gunicorn and I see that tip every time I access the flask binary, even just outputting the help menu, routes, etc.. All I have is a single .env file in the root of my project.

@davidism
Copy link
Member

davidism commented Jan 18, 2021

Ah, you're right, the cli in general checks for the file. I'm not going to remove the message right now, so your best bet is setting the extra environment variable. You could automate this if you use something like cookiecutter to create new projects from templates.

@nickjj
Copy link
Contributor Author

nickjj commented Jan 18, 2021

Sure it can be automated in development, but it means setting it in every project and in production chances are you're not committing your .env which means you need to explicitly add FLASK_SKIP_DOTENV=true to whatever production environment you're deploying to.

That could be adding it through the Heroku CLI or editing whatever env variables / env file / secrets mechanism you use with Docker Compose, systemd, Kubernetes, ECS, Swarm, DigitalOcean Apps or the half dozen other application runtimes that all load env variables in various ways without needing python-dotenv.

Edit:

Ah, you're right, the cli in general checks for the file. I'm not going to remove the message right now

The only reason I'm pushing back on this is because it's an inconvenience to every person running Flask in production since the tip comes up every time you run any flask command.

If it worked as you originally described by only being output with flask run then it would be no big deal since no one would ever see it unless you were in development using flask run. But since we determined it's a bug and not working like that, maybe as a compromise you can make the warning only show up in the case you originally intended it for? That solves the problem too.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants