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

时间:2023-02-02 22:45:14

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

Before you start downvoting, 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.

在你开始投反对票之前,我读过这篇文章,但这并不能解决我想要的问题。此外,我尝试过在首选项中设置环境变量—>构建、执行、部署—>控制台—>Python控制台/Django控制台,但是它为解释器设置了变量。

2 个解决方案

#1


28  

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.

首选>插件>浏览存储库…>搜索“Env文件”>安装插件。

Then, I created a file, in my project root, called environment.env which 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:

然后我去运行->编辑配置,按照下一个图像中的步骤:

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

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中,我选择了文件环境。env,然后我就可以点击PyCharm里的播放按钮,所有的东西都很好用。

#2


25  

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

您可以在Pycharm的run configurations菜单中设置环境变量。

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

    在右上角打开运行配置选择器,然后点击编辑配置…

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

  2. Find Environmental variables and click ...

    找到环境变量并点击…

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

  3. Add or change variables, then click OK

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

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

You can access your environmental variables with os.environ

您可以使用os. environment访问您的环境变量

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

#1


28  

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.

首选>插件>浏览存储库…>搜索“Env文件”>安装插件。

Then, I created a file, in my project root, called environment.env which 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:

然后我去运行->编辑配置,按照下一个图像中的步骤:

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

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中,我选择了文件环境。env,然后我就可以点击PyCharm里的播放按钮,所有的东西都很好用。

#2


25  

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

您可以在Pycharm的run configurations菜单中设置环境变量。

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

    在右上角打开运行配置选择器,然后点击编辑配置…

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

  2. Find Environmental variables and click ...

    找到环境变量并点击…

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

  3. Add or change variables, then click OK

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

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

You can access your environmental variables with os.environ

您可以使用os. environment访问您的环境变量

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