无法在Windows上使用Python脚本激活conda环境

huangapple go评论65阅读模式
英文:

Can't activate a conda environment in windows using a Python script

问题

I am running Windows 10 Pro version 22H2 and miniconda 23.3.1. Running this on windows is a requirment for me.

我正在运行Windows 10专业版22H2和miniconda 23.3.1。在Windows上运行对我来说是必需的。

I am trying to activate a conda environment from a python script with the following code:

我正在尝试使用以下代码从Python脚本中激活conda环境:

import os
os.chdir("C:/Users/name/miniconda3/Scripts")
os.system("conda activate TestEnv")

My script is able to successfully change directories, but whenever it tries to actually activate the environment I get the error:

我的脚本能够成功更改目录,但每当它尝试实际激活环境时,我会收到以下错误:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

I know that there are other stack overflows on this exact error, but most of them are on Linux, and the ones that were on windows didn't have solutions that worked for me.

我知道关于这个确切错误的其他堆栈溢出问题,但其中大多数是关于Linux的,而那些关于Windows的并没有适用于我的解决方案。

The frustrating part is that if I copy and paste the commands that my script is running and run them manually everything works with no errors, and I am put into "TestEnv". It just doesn't work with the script. I've already initialized all of the shells, and I've tried other variations of the "conda activate" command such as:

令人沮丧的部分是,如果我复制并粘贴我的脚本正在运行的命令,并手动运行它们,一切都可以正常工作,没有错误,并且我会进入"TestEnv"。只是脚本不起作用。我已经初始化了所有的shell,并尝试了其他"conda activate"命令的变体,比如:

  • activate TestEnv(在终端中有效,但在脚本中不起作用)
  • conda run -n TestEnv(没有错误,只是不起作用)
  • conda run -p C:\\Users\\Axion\\miniconda3\\envs\\TestEnv(没有错误,只是不起作用)

Edit:

My goal is to activate the environment and then run a python script within it. The final code might look more like this:

编辑:

我的目标是激活环境,然后在其中运行一个Python脚本。最终的代码可能如下所示:

import os
os.chdir("C:/Users/name/miniconda3/Scripts")
os.system("conda activate TestEnv")
os.system("python script1.py")
os.system("conda activate TestEnv2")
os.system("python script2.py")
英文:

I am running Windows 10 Pro version 22H2 and miniconda 23.3.1. Running this on windows is a requirment for me.

I am trying to activate a conda environment from a python script with the following code:

import os
os.chdir(&quot;C:/Users/name/miniconda3/Scripts&quot;)
os.system(&quot;conda activate TestEnv&quot;)

My script is able to successfully change directories, but whenever it tries to actually activate the environment I get the error:

CommandNotFoundError: Your shell has not been properly configured to use &#39;conda activate&#39;.
If using &#39;conda activate&#39; from a batch script, change your
invocation to &#39;CALL conda.bat activate&#39;.

To initialize your shell, run

    $ conda init &lt;SHELL_NAME&gt;

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See &#39;conda init --help&#39; for more information and options.

IMPORTANT: You may need to close and restart your shell after running &#39;conda init&#39;.

I know that there are other stack overflows on this exact error, but most of them are on Linux, and the ones that were on windows didn't have solutions that worked for me.

The frustrating part is that if I copy and paste the commands that my script is running and run them manually everything works with no errors, and I am put into "TestEnv". It just doesn't work with the script. I've already initialized all of the shells, and I've tried other variations of the "conda activate" command such as:

  • activate TestEnv (works in terminal, but not in script)
  • conda run -n TestEnv (no errors just doesn't work)
  • conda run -p C:\\Users\\Axion\\miniconda3\\envs\\TestEnv (no errors, just doesn't work)

Edit:

My goal is to activate the environment and then run a python script within it. The final code might look more like this:

import os
os.chdir(&quot;C:/Users/name/miniconda3/Scripts&quot;)
os.system(&quot;conda activate TestEnv&quot;)
os.system(&quot;python script1.py&quot;)
os.system(&quot;conda activate TestEnv2&quot;)
os.system(&quot;python script2.py&quot;)

答案1

得分: 1

"conda run"命令不是用于激活环境,而是用于在环境中运行命令。所以,你想要:

import os

## 在TestEnv中运行script1
os.system("conda run -n TestEnv python script1.py")
## 在TestEnv2中运行script2
os.system("conda run -n TestEnv2 python script2.py")
英文:

The conda run command is not to activate an environment, it is to direct a command to run within an environment. So, you want:

import os

## run script1 in TestEnv
os.system(&quot;conda run -n TestEnv python script1.py&quot;)
## run script2 in TestEnv2
os.system(&quot;conda run -n TestEnv2 python script2.py&quot;)

huangapple
  • 本文由 发表于 2023年6月13日 02:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定