0

I'm exploring ubuntu and for a small project, I'm writing a .sh(bash) file that will activate a Conda environment and run a python file. Here is my .sh file:

#!/bin/sh  
conda activate simple_python3.10
python3 code.py

When I run the bash file it conda gives me this warning:

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

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

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

At this time I don't know what exactly I should do. I did conda init bash but didn't work. I added conda init bash to my file but it didn't work. This warning showed up once again. I rebooted my PC too but didn't work. Could you please tell me what exactly I should do?

P.S. Just for the case. This is what I will get after running conda init bash:

no change     /home/p2mohsen/anaconda3/condabin/conda
no change     /home/p2mohsen/anaconda3/bin/conda
no change     /home/p2mohsen/anaconda3/bin/conda-env
no change     /home/p2mohsen/anaconda3/bin/activate
no change     /home/p2mohsen/anaconda3/bin/deactivate
no change     /home/p2mohsen/anaconda3/etc/profile.d/conda.sh
no change     /home/p2mohsen/anaconda3/etc/fish/conf.d/conda.fish
no change     /home/p2mohsen/anaconda3/shell/condabin/Conda.psm1
no change     /home/p2mohsen/anaconda3/shell/condabin/conda-hook.ps1
no change     /home/p2mohsen/anaconda3/lib/python3.9/site-packages/xontrib/conda.xsh
no change     /home/p2mohsen/anaconda3/etc/profile.d/conda.csh
no change     /home/p2mohsen/.bashrc
No action taken.
Peyman
  • 3,097
  • 5
  • 33
  • 56
  • Did you do? `IMPORTANT: You may need to close and restart your shell after running '` – Jetchisel Dec 25 '22 at 07:23
  • @Jetchisel I forgot to say I even restarted my laptop but didn't work. – Peyman Dec 25 '22 at 08:12
  • FYI, if you want a *bash* script, then probably should not be using an *sh* shebang: https://stackoverflow.com/q/10376206/570918 – merv Dec 26 '22 at 06:33

1 Answers1

3

It seems you can't just do conda activate inside a bash file. you should add the line eval "$(conda shell.bash hook)" before calling it.

So I changed my .sh file to this and it worked:

#!/bin/sh  
eval "$(conda shell.bash hook)"
conda activate simple_python3.10
python3 code.py
Peyman
  • 3,097
  • 5
  • 33
  • 56