conda icon indicating copy to clipboard operation
conda copied to clipboard

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

Open santanu-c opened this issue 1 year ago • 10 comments

What happened?

conda activate <my environment> from a shell script is giving following error:

./sc2.sh activate /root/demo10

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

A brief background and what was done:

  1. Installed miniconda successfully by downloading this file "https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh".

  2. I see that conda init adds lines in .bashrc and I see base is activated after I restart the shell.

  3. Scripts:

    sc2.sh
    #!/bin/bash
    
    echo "I am running sc2.sh: params: $@"
    #. /opt/miniconda/dist/etc/profile.d/conda.sh
    
    conda $@
    /root/demo.py
    
    demo.py
    #!/usr/bin/env python
    
    import os
    import sys
    
    print("Python version:", sys.version)
    

Conda Details

conda info
     active environment : base
    active env location : /opt/miniconda/dist
            shell level : 1
       user config file : /root/.condarc
 populated config files :
          conda version : 23.5.2
    conda-build version : not installed
         python version : 3.11.4.final.0
       virtual packages : __archspec=1=x86_64
                          __glibc=2.28=0
                          __linux=4.18.0=0
                          __unix=0=0
       base environment : /opt/miniconda/dist  (writable)
      conda av data dir : /opt/miniconda/dist/etc/conda
  conda av metadata url : None
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /opt/miniconda/dist/pkgs
                          /root/.conda/pkgs
       envs directories : /opt/miniconda/dist/envs
                          /root/.conda/envs
               platform : linux-64
             user-agent : conda/23.5.2 requests/2.29.0 CPython/3.11.4 Linux/4.18.0-305.el8.x86_64 rhel/8.4 glibc/2.28
                UID:GID : 0:0
             netrc file : None
           offline mode : False
conda config
==> /opt/miniconda/dist/condarc <==
create_default_packages:
  - python
  - pip
  - ipython
  - cryptography
channels:
  - https://<hidden>.jfrog.io/artifactory/api/conda/my-conda
always_yes: True

==> /root/.condarc <==
show_channel_urls: True
conda list
# packages in environment at /opt/miniconda/dist:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main    defaults
_openmp_mutex             5.1                       1_gnu    defaults
boltons                   23.0.0          py311h06a4308_0    defaults
brotlipy                  0.7.0           py311h5eee18b_1002    defaults
bzip2                     1.0.8                h7b6447c_0    defaults
c-ares                    1.19.0               h5eee18b_0    defaults
ca-certificates           2023.05.30           h06a4308_0    defaults
certifi                   2023.5.7        py311h06a4308_0    defaults
cffi                      1.15.1          py311h5eee18b_3    defaults
charset-normalizer        2.0.4              pyhd3eb1b0_0    defaults
conda                     23.5.2          py311h06a4308_0    defaults
conda-content-trust       0.1.3           py311h06a4308_0    defaults
conda-libmamba-solver     23.5.0          py311h06a4308_0    defaults
conda-package-handling    2.1.0           py311h06a4308_0    defaults
conda-package-streaming   0.8.0           py311h06a4308_0    defaults
cryptography              39.0.1          py311h9ce1e76_2    defaults
fmt                       9.1.0                hdb19cb5_0    defaults
icu                       58.2                 he6710b0_3    defaults
idna                      3.4             py311h06a4308_0    defaults
jsonpatch                 1.32               pyhd3eb1b0_0    defaults
jsonpointer               2.1                pyhd3eb1b0_0    defaults
krb5                      1.20.1               h143b758_1    defaults
ld_impl_linux-64          2.38                 h1181459_1    defaults
libarchive                3.6.2                h6ac8c49_2    defaults
libcurl                   8.1.1                h251f7ec_1    defaults
libedit                   3.1.20221030         h5eee18b_0    defaults
libev                     4.33                 h7f8727e_1    defaults
libffi                    3.4.4                h6a678d5_0    defaults
libgcc-ng                 11.2.0               h1234567_1    defaults
libgomp                   11.2.0               h1234567_1    defaults
libmamba                  1.4.1                h2dafd23_1    defaults
libmambapy                1.4.1           py311h2dafd23_1    defaults
libnghttp2                1.52.0               h2d74bed_1    defaults
libsolv                   0.7.22               he621ea3_0    defaults
libssh2                   1.10.0               hdbd6064_2    defaults
libstdcxx-ng              11.2.0               h1234567_1    defaults
libuuid                   1.41.5               h5eee18b_0    defaults
libxml2                   2.10.3               hcbfbd50_0    defaults
lz4-c                     1.9.4                h6a678d5_0    defaults
ncurses                   6.4                  h6a678d5_0    defaults
openssl                   3.0.9                h7f8727e_0    defaults
packaging                 23.0            py311h06a4308_0    defaults
pcre2                     10.37                he7ceb23_1    defaults
pip                       23.1.2          py311h06a4308_0    defaults
pluggy                    1.0.0           py311h06a4308_1    defaults
pybind11-abi              4                    hd3eb1b0_1    defaults
pycosat                   0.6.4           py311h5eee18b_0    defaults
pycparser                 2.21               pyhd3eb1b0_0    defaults
pyopenssl                 23.0.0          py311h06a4308_0    defaults
pysocks                   1.7.1           py311h06a4308_0    defaults
python                    3.11.4               h955ad1f_0    defaults
readline                  8.2                  h5eee18b_0    defaults
reproc                    14.2.4               h295c915_1    defaults
reproc-cpp                14.2.4               h295c915_1    defaults
requests                  2.29.0          py311h06a4308_0    defaults
ruamel.yaml               0.17.21         py311h5eee18b_0    defaults
setuptools                67.8.0          py311h06a4308_0    defaults
six                       1.16.0             pyhd3eb1b0_1    defaults
sqlite                    3.41.2               h5eee18b_0    defaults
tk                        8.6.12               h1ccaba5_0    defaults
toolz                     0.12.0          py311h06a4308_0    defaults
tqdm                      4.65.0          py311h92b7b1e_0    defaults
tzdata                    2023c                h04d1e81_0    defaults
urllib3                   1.26.16         py311h06a4308_0    defaults
wheel                     0.38.4          py311h06a4308_0    defaults
xz                        5.4.2                h5eee18b_0    defaults
yaml-cpp                  0.7.0                h295c915_1    defaults
zlib                      1.2.13               h5eee18b_0    defaults
zstandard                 0.19.0          py311h5eee18b_0    defaults
zstd                      1.5.5                hc292b87_0    defaults

Additional Context

I searched all over the internet and found most useful steps here: https://github.com/conda/conda/issues/7980 https://donny-son.github.io/posts/cronjob-with-conda/

But looks like nothing is working.

santanu-c avatar Aug 18 '23 02:08 santanu-c

Hello, Any update please?

santanu-c avatar Aug 20 '23 01:08 santanu-c

For conda activate to work properly it needs to be initialized as a shell function. This is done by the code injected into your user profile (e.g., .profile) when you run conda init. Unfortunately, by default, when running a script your user profile is not loaded. You'll need to update the script's hashbang to load your profile:

#!/bin/bash --login
...

kenodegard avatar Aug 20 '23 01:08 kenodegard

What am I missing?.. this is what I did...

sc2.sh
#!/bin/bash --login

#eval "$(conda shell.bash hook)"
conda $@

#/root/demo.py
echo "+++++++++++++++++++++++++++++"
conda env list
# conda env list  
# conda environments:
#
base                  *  /opt/miniconda/dist
                         /root/demo10
                         /root/demo20
                         /root/demo30

Now I run the following:

(base) $ ./sc2.sh activate /root/demo20
+++++++++++++++++++++++++++++
(base) $

See that the current environment is not pointing to /root/demo20:

$ conda env list
# conda environments:
#
base                  *  /opt/miniconda/dist
                         /root/demo10
                         /root/demo20
                         /root/demo30

Another case:

$ /opt/miniconda/dist/bin/conda env list
# conda environments:
#
base                  *  /opt/miniconda/dist
                         /root/demo10
                         /root/demo20
                         /root/demo30

Now see below

$ /opt/miniconda/dist/bin/conda activate /root/demo30

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

But this works... (the asterisk is now pointed to /root/demo30):

$ conda activate /root/demo30
(/root/demo30) $ conda env list
# conda environments:
#
base                     /opt/miniconda/dist
                         /root/demo10
                         /root/demo20
                      *  /root/demo30

santanu-c avatar Aug 20 '23 02:08 santanu-c

Is there any trick to make it work ? To me it seems that miniconda is dead on arrival at this point!

To an application developer this is the normal flow...

  1. create the application script (lets say myapp1.py)
  2. Test it in different python versions.
  3. capture the mudule dependencies in the yml format (lets say myapp.yml)
  4. Create a wrapper (lets say deployapp.py)
  5. Check in the code myapp1.py, myapp.yml and deplyapp.py into the code repo.
  6. To deploy myapp1 in target machine for production, we run deployapp.py and that does these steps a. copy all files in target machine b. create the target environment using the myapp.yml c. activate the environment d. run the code myapp1.py in the activated environment.

So the qn is if 6c is not working then how would someone use miniconda? Does anyone have answer to it?

santanu-c avatar Aug 20 '23 14:08 santanu-c

Any help , conda team ?

santanu-c avatar Aug 21 '23 17:08 santanu-c

Based on what I see you're doing, everything is working correctly.

The conda command is a shell function that processes activation subcommands (e.g., conda activate) differently from other subcommands (if your default shell is bash see type conda, if your default shell is zsh see which conda). What's why conda activate works but specifying the full path /opt/miniconda/dist/bin/conda activate does not.

Experiment with this modified script to more clearly see which environment you have activated:

#!/bin/bash --login

conda $@

echo "+++++++++++++++++++++++++++++"
echo "active environment: $CONDA_PREFIX"
echo "+++++++++++++++++++++++++++++"

With this modified script we see the following:

(base) $ ./sc2-modified.sh activate /root/demo20
+++++++++++++++++++++++++++++
activate environment: /root/demo20
+++++++++++++++++++++++++++++
(base) $

Notice that within your script you have activated the correct environment and once the script completes you are returned to your previous environment. This is expected.

Your deployapp.sh script likely needs to look like:

#!/bin/bash --login

# create application environment
conda env create --prefix /root/myapp --file path/to/myapp.yml --yes --quiet

# activate application environment
conda activate /root/myapp

# optionally inspect conda, the active environment will be listed
conda info

# run application
path/to/myapp1.py

kenodegard avatar Aug 21 '23 17:08 kenodegard

Hi there, thank you for your contribution!

This issue has been automatically marked as stale because it has not had recent activity. It will be closed automatically if no further activity occurs.

If you would like this issue to remain open please:

  1. Verify that you can still reproduce the issue at hand
  2. Comment that the issue is still reproducible and include: - What OS and version you reproduced the issue on - What steps you followed to reproduce the issue

NOTE: If this issue was closed prematurely, please leave a comment.

Thanks!

github-actions[bot] avatar Nov 23 '23 04:11 github-actions[bot]

Hi there, I got the same issue recently and solve this problem by running codes below: #!/bin/bash --login source ~/.bashrc conda init --all conda activate ~/miniconda3/envs/your_env_name Hope it will help.

lexuziandyu avatar Dec 07 '23 02:12 lexuziandyu

Hi there, I got the same issue recently and solve this problem by running codes below: #!/bin/bash --login source ~/.bashrc conda init --all conda activate ~/miniconda3/envs/your_env_name Hope it will help.

this works, thanks!

guanyuanm avatar Feb 16 '24 17:02 guanyuanm

Thank you very much.

aakashsyadav1999 avatar Mar 08 '24 06:03 aakashsyadav1999

thanks!

Prod1us avatar Apr 07 '24 16:04 Prod1us