简体   繁体   English

VS Code 不使用 pipenv .env 文件

[英]VS Code doesn't use pipenv .env file

Working with VS Code 1.35.1 on ubuntu 16.04 with a Python 3.7.3 pipenv virtual environment, I am trying to set environment variables in a .env file, but for some reason the file doesn't seem to be recognized.在带有 Python 3.7.3 pipenv 虚拟环境的 ubuntu 16.04 上使用 VS Code 1.35.1,我试图在 .env 文件中设置环境变量,但由于某种原因,该文件似乎无法识别。

Can someone help me understand what I can do to give my (Django) app access to the environment variables, without needing to manually run pipenv shell .有人可以帮助我了解如何让我的(Django)应用程序访问环境变量,而无需手动运行pipenv shell

Steps taken:采取的步骤:
So, this is what I am doing exactly:所以,这就是我正在做的事情:

1 - I have set the Python interpreter for my project like so: ctrl + shift + p > Python: Select interpreter > Python 3.7.3 64-bit ('environment_name': pipenv) 1 - 我已经为我的项目设置了 Python 解释器,如下所示: ctrl + shift + p > Python: Select interpreter > Python 3.7.3 64-bit ('environment_name': pipenv)

2 - Created a .env file inside the project root directory: 2 - 在项目根目录中创建一个 .env 文件:

# Django
SECRET_KEY="some key here"
DEBUG=True

...

3 - Made sure the VS Code Python extension is installed and enabled 3 - 确保安装并启用了 VS Code Python 扩展

4 - Adjusted my Django settings file to get the SECRET_KEY from the environment variables: 4 - 调整我的 Django 设置文件以从环境变量中获取 SECRET_KEY:

SECRET_KEY = os.getenv('SECRET_KEY')

5 - Running the Django development server from the VS Code terminal (with pipenv environment activated through ctrl + shift + ~ ): 5 - 从 VS Code 终端运行 Django 开发服务器(通过ctrl + shift + ~激活 pipenv 环境):

(environment-name) user@system-name:~/projects/my-project$ python manage.py runserver

6 - No other settings changed 6 - 没有更改其他设置

I haven't changed any settings, like the python.envFile setting.我没有更改任何设置,例如python.envFile设置。 Settings are left to their defaults.设置保留为默认值。

How I know that the .env file isn't recognized:我怎么知道 .env 文件无法识别:

The above steps result in the following message in the VS Code terminal:上述步骤会在 VS Code 终端中产生以下消息:

...
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

While if I run pipenv shell inside the same VS Code terminal (with the already activated environment, and thus activating it again), and start the Django dev server: python manage.py runserver , the server starts perfectly.如果我在同一个 VS Code 终端中运行pipenv shell (使用已经激活的环境,因此再次激活它),并启动 Django 开发服务器: python manage.py runserver ,服务器将完美启动。

It looks like your interpreter is not picking up pipenv environment看起来你的解释器没有选择 pipenv 环境

1) check pipenv --where 1) 检查 pipenv --where

2) see if in your settings.json file that your pythonPath variable is set to the location {pipenv --where}/bin/python{some version} 2)查看您的 settings.json 文件中是否将您的 pythonPath 变量设置为位置 {pipenv --where}/bin/python{some version}

3) run again 3)再次运行

Looks like this issue is still open in the vscode-python GitHub.貌似这个问题还在vscode-python GitHub 中打开。 Two solutions would be: a) to run .env separate from your pipenv activation;两种解决方案是:a) 将 .env 与 pipenv 激活分开运行; or b) follow the steps in here .或 b) 按照此处的步骤操作。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM