简体   繁体   中英

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. Using macOS Big Sur 11.6, bash in Terminal (not zsh).

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. Nothing fancy. Doing it manually works fine, but once I put it in a shell script, it won't work. 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.

I tried running: bash -i setup.sh but it still does not activate. I get no errors but I'm still stuck in (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. No errors again, but stuck in (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. 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:

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 .

Even tried adding #!/bin/bash to the top of the file just in case, but no dice.

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. Any help will be appreciated.

Thanks to @fravadona for the simplest of solutions.

Simply executing the script with source instead of sh . 🤦🏻‍♂️

Final setup.sh script (with my preliminary pip installs):

# 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.

Still not sure why it won't work by adding other things to the shell script, but this is still a great, simple solution. And yes, I am a novice with this stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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