简体   繁体   English

在windows中激活python虚拟环境时设置环境变量

[英]Set environment variables when activating python virtual environment in windows

I want to be able to set up environment variables in my virtual environment so that they are available in my code when I activate the virtual environment.我希望能够在我的虚拟环境中设置环境变量,以便在我激活虚拟环境时它们在我的代码中可用。 I make my virtual enviornments with venv.我用 venv 制作我的虚拟环境。 I'm working on a Windows machine with VS-code.我正在使用 VS 代码的 Windows 机器上工作。

What I already tried, but didn't work.我已经尝试过,但没有奏效。

  1. Adding the vars to end of the activate.bat file like this:将变量添加到 activate.bat 文件的末尾,如下所示:
set CLIENT_SECRET="MYSECRET"
  1. Adding the vars to the end of the Activate.ps1 file like this:将变量添加到 Activate.ps1 文件的末尾,如下所示:
$CLIENT_SECRET="MYSECRET"
  1. Adding the vars to the end of the activate file like this:将变量添加到激活文件的末尾,如下所示:
export CLIENT_SECRET="MYSECRET"

I found a lot related to my topic, but none working for me.我发现了很多与我的主题相关的内容,但没有一个对我有用。 What to do?该怎么办?

If you want to setup your development environment in VSCode you can simply add .env file with all secrets defined in project root directory.如果你想在 VSCode 中设置你的开发环境,你可以简单地添加.env文件,其中包含在项目根目录中定义的所有秘密。 More details in docs 文档中的更多详细信息

Your first solution您的第一个解决方案

set CLIENT_SECRET=MYSECRET

in activate.bat should work, when using Command Prompt in the terminal as Default Shell.当在终端中使用命令提示符作为默认 Shell 时, activate.bat中的应该可以工作。

You can omit the quotes unless they are part of your envirionment variable.您可以省略引号,除非它们是您的环境变量的一部分。

You can verify, if the environment variable is set with:您可以验证是否设置了环境变量:

echo %CLIENT_SECRET% in the terminal in VS-Code.在 VS-Code 的终端中echo %CLIENT_SECRET%

转到禀赋变量文件夹输入脚本文件夹现在用 cmd 激活

在 CMD 终端中使用 set 使用 env: in powershell

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

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