Terminal not loading environmental variables with PIPENV
Answered
I'm working on a Flask app, using pipenv. I have a .env file with FLASK_APP=ccadmin.py defined.
The problem is that when I run flask shell in the PyCharm Terminal, it's not loading the .env environmental variables. I have to type "set FLASK_APP = ccadmin.py" every single time.
However if I run "pipenv shell" from the Windows CLI, everything loads correctly.
What's more... if I run "pipenv shell" from the PyCharm Terminal, it also loads the .env environmental variables correctly.
Is the PyCharm terminal not invoking pipenv?
I'm running the latest PyCharm 2018.3
What additional information should I provide to help?
From PyCharm Terminal....
This does NOT work
Microsoft Windows [Version 10.0.16299.785]
(c) 2017 Microsoft Corporation. All rights reserved.
(CC Admin Site) C:\Users\davida\Dropbox\CC Admin Site>flask shell
* Tip: There are .env files present. Do "pip install python-dotenv" to use them.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
App: app [production]
Instance: C:\Users\davida\Dropbox\CC Admin Site\instance
>>> app
<Flask 'app'>
>>> db
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'db' is not defined
This DOES work
Microsoft Windows [Version 10.0.16299.785]
(c) 2017 Microsoft Corporation. All rights reserved
(CC Admin Site) C:\Users\davida\Dropbox\CC Admin Site>pipenv shell
Loading .env environment variables…
Launching subshell in virtual environment…
Microsoft Windows [Version 10.0.16299.785]
(c) 2017 Microsoft Corporation. All rights reserved.
(CC_Admin_Site-0g0SztUB) (CC Admin Site) C:\Users\davida\Dropbox\CC Admin Site>flask shell
* Tip: There are .env files present. Do "pip install python-dotenv" to use them.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
App: app [production]
Instance: C:\Users\davida\Dropbox\CC Admin Site\instance
>>> app
<Flask 'app'>
>>> db
<SQLAlchemy engine=postgresql://cc_web_admin:***@localhost/postgres>
I created the project to use Pipenv
Please sign in to leave a comment.
Hi David,
PyCharm's Terminal doesn't support working with .env files. You should probably try setting FLASK_APP environment variable in Terminal settings Settings | Tools | Terminal.
@Sergey,
Pipenv load .env by default when running `pipenv shell` or `pipenv run`. PyCharm does not need to deal with .env files on its own. It would be nice if PyCharm could use pipenv properly and allow developers to use .env file as one could with a plain terminal.
Please vote for https://youtrack.jetbrains.com/issue/PY-31111
This bug still exists with pycharm 2020.2. The workaround for now is to deactivate the virtual environment with `deactivate` then rerun `pipenv shell`. It's annoying I have to do this whenever I open a new terminal, so hopefully this gets fixed.