Python 如何在 PyCharm 中设置环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42708389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:03:31  来源:igfitidea点击:

How to set environment variables in PyCharm?

pythondjangoenvironment-variables

提问by lmiguelvargasf

I have started to work in a Django project, and I would like to set some environment variables without having to set them manually or having a bash file to source.

我已经开始在一个 Django 项目中工作,我想设置一些环境变量,而不必手动设置它们或有 bash 文件作为源。

I would like to set the following variables:

我想设置以下变量:

export DATABASE_URL=postgres://127.0.0.1:5432/my_db_name
export DEBUG=1
# there are other variables, but they contain personal information

I have read this, but that does not solve what I want. In addition, I have tried setting the environment variables in Preferences-> Build, Execution, Deployment->Console->Python Console/Django Console, but it sets the variables for the interpreter.

我已经阅读了这个,但这并不能解决我想要的问题。此外,我尝试在Preferences-> Build, Execution, Deployment-> Console-> Python Console/ Django Console 中设置环境变量,但它为解释器设置了变量。

回答by Joran Beasley

You can set environmental variables in Pycharm's run configurations menu.

您可以在 Pycharm 的运行配置菜单中设置环境变量。

  1. Open the Run Configuration selector in the top-right and cick Edit Configurations...

    Edit Configurations...

  2. Find Environmental variablesand click ...

    Environmental variables

  3. Add or change variables, then click OK

    Editing environmental variables

  1. 打开右上角的运行配置选择器,然后单击 Edit Configurations...

    编辑配置...

  2. 查找Environmental variables并点击...

    环境变量

  3. 添加或更改变量,然后单击 OK

    编辑环境变量

You can access your environmental variableswith os.environ

您可以访问你的环境变量os.environ

import os
print(os.environ['SOME_VAR'])

回答by lmiguelvargasf

I was able to figure out this using a PyCharm plugin called EnvFile. This plugin, basically allows setting environment variables to run configurations from one or multiple files.

我能够使用名为EnvFile的 PyCharm 插件来解决这个问题。这个插件基本上允许设置环境变量以从一个或多个文件运行配置。

The installation is pretty simple:

安装非常简单:

Preferences> Plugins> Browse repositories...> Search for "Env File"> Install Plugin.

Preferences> Plugins> Browse repositories...> Search for "Env File"> Install Plugin

Then, I created a file, in my project root, called environment.envwhich contains:

然后,我在我的项目根目录中创建了一个文件,名为environment.env,其中包含:

DATABASE_URL=postgres://127.0.0.1:5432/my_db_name
DEBUG=1

Then I went to Run->Edit Configurations, and I followed the steps in the next image:

然后我转到Run-> Edit Configurations,并按照下图中的步骤操作:

Set Environment Variables

设置环境变量

In 3, I chose the file environment.env, and then I could just click the play button in PyCharm, and everything worked like a charm.

在 3 中,我选择了文件environment.env,然后我可以在 PyCharm 中单击播放按钮,一切都像魅力一样。

回答by Nino van Hooff

This functionality has been added to the IDE now (working Pycharm 2018.3)

此功能现已添加到 IDE(工作 Pycharm 2018.3)

Just click the EnvFiletab in the run configuration, click Enable EnvFileand click the + icon to add an env file

只需单击EnvFile运行配置中的选项卡,单击Enable EnvFile并单击 + 图标即可添加 env 文件

Example with the glorious material theme

具有光辉材质主题的示例

Update: Essentially the same as the answer by @imguelvargasf but the the plugin was enabled by default for me.

更新:本质上与@imguelvargasf 的答案相同,但该插件默认为我启用。