简体   繁体   English

无法从 shell 脚本 macOS、bash 激活 conda env

[英]Can't activate conda env from shell script macOS, bash

I've tried everything I've seen on SO to get this to work, but so far everything fails.我已经尝试了所有我在 SO 上看到的方法来让它工作,但到目前为止一切都失败了。 Using macOS Big Sur 11.6, bash in Terminal (not zsh).使用 macOS Big Sur 11.6,在终端(不是 zsh)中进行 bash。

I'm trying to create a setup file and execute with sh setup.sh that will setup the env, install python, and then activate it.我正在尝试创建一个安装文件并使用sh setup.sh执行,它将设置环境,安装 python,然后激活它。 Nothing fancy.没有什么花哨。 Doing it manually works fine, but once I put it in a shell script, it won't work.手动执行它可以正常工作,但是一旦我将其放入 shell 脚本中,它将无法正常工作。 I'm running this script from inside an empty project folder.我从一个空的项目文件夹中运行这个脚本。

Current script:当前脚本:

conda create -n MASTER python=3.8.5 -y

conda activate MASTER

Yeah, it's that simple to start with.是的,开始就是这么简单。 I commented out the other pip installs until this works properly.我注释掉了其他 pip 安装,直到它正常工作。

I tried running: bash -i setup.sh but it still does not activate.我尝试运行: bash -i setup.sh但它仍然没有激活。 I get no errors but I'm still stuck in (base) .我没有收到任何错误,但我仍然卡在(base)

I tried using source: source /opt/anaconda3/etc/profile.d/conda.sh at beginning of script and/or before activate, still doesn't work.我尝试在脚本开始和/或激活之前使用 source: source /opt/anaconda3/etc/profile.d/conda.sh ,但仍然不起作用。 No errors again, but stuck in (base) .再次没有错误,但卡在(base)

I tried using: eval $(conda shell.bash hook) at the start of script and before I try to activate the env, but it fails.我尝试在脚本开始和尝试激活 env 之前使用: eval $(conda shell.bash hook) ,但它失败了。 This time I get the error:这次我收到错误:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

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

But if I run conda init bash (in Terminal or in the script itself), it outputs:但是,如果我运行conda init bash (在终端或脚本本身中),它会输出:

no change     /opt/anaconda3/condabin/conda
no change     /opt/anaconda3/bin/conda
no change     /opt/anaconda3/bin/conda-env
no change     /opt/anaconda3/bin/activate
no change     /opt/anaconda3/bin/deactivate
no change     /opt/anaconda3/etc/profile.d/conda.sh
no change     /opt/anaconda3/etc/fish/conf.d/conda.fish
no change     /opt/anaconda3/shell/condabin/Conda.psm1
no change     /opt/anaconda3/shell/condabin/conda-hook.ps1
no change     /opt/anaconda3/lib/python3.8/site-packages/xontrib/conda.xsh
no change     /opt/anaconda3/etc/profile.d/conda.csh
no change     /Users/liquidRock/.bash_profile
No action taken.

I tried doing /opt/anaconda3/bin/conda activate MASTER which also prompts me to do conda init bash .我尝试做/opt/anaconda3/bin/conda activate MASTER这也提示我做conda init bash

Even tried adding #!/bin/bash to the top of the file just in case, but no dice.甚至尝试将#!/bin/bash到文件顶部以防万一,但没有骰子。

At a loss here and haven't found any other methods.在这里不知所措,还没有找到任何其他方法。 I just want to be able to run my setup script and have it install python and activate the env.我只是希望能够运行我的安装脚本并让它安装 python 并激活 env。 Any help will be appreciated.任何帮助将不胜感激。

Thanks to @fravadona for the simplest of solutions.感谢@fravadona 提供了最简单的解决方案。

Simply executing the script with source instead of sh .只需使用source而不是sh执行脚本。 🤦🏻‍♂️ 🤦🏻‍♂️

Final setup.sh script (with my preliminary pip installs):最终 setup.sh 脚本(使用我的初步 pip 安装):

# env & python
conda create -n MASTER python=3.8.5 -y
conda activate MASTER

# pip installs
pip install cmake
pip install --upgrade pip setuptools wheel
pip install opencv-python==4.2.0.32
pip install argparse
pip install datetime
pip install colorama
pip install python-dotenv
pip install python-dotenv[cli]

Executed thusly:如此执行:

$ source setup.sh

Anaconda creates the env, installs python and dependencies, activates the env, then pip installs the additional dependencies. Anaconda 创建 env,安装 python 和依赖项,激活 env,然后 pip 安装其他依赖项。

Still not sure why it won't work by adding other things to the shell script, but this is still a great, simple solution.仍然不确定为什么它不能通过向 shell 脚本添加其他内容来工作,但这仍然是一个很棒的简单解决方案。 And yes, I am a novice with this stuff.是的,我是这方面的新手。

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

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