I would like to ssh into a remote server and change the conda environment - conda won't let me do that.
Either the conda command is not found and if I explicitely give the path to conda or add the path manually, it wants me too re-initialize the shell with conda init bash. Re-initialization of course does not change the error message. What can I do to use conda via ssh.
(Doing the steps by hand does not lead to an error).
sshpass -p 'passwort' ssh root#999.999.999.999 "export PATH='/path/to/anaconda3/bin:$PATH'; conda activate main"
# yields:
#CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
#To initialize your shell, run $ conda init <SHELL_NAME>
#Although it is working very well when manually ssh-ing to the remote server
sshpass -p 'passwort' ssh root#999.999.999.999 "/path/to/anaconda3/bin/conda activate main"
# Same error
sshpass -p 'passwort' ssh root#999.999.999.999 "conda activate main"
# bash: conda: command not found
There is a similiar question
python - Conda command not found - Stack Overflow
but it does not cover the problem of re-initialization
After installing any conda package, login name disappears from Linux terminal. For reference please see screenshot.
Looks like you installed conda 22.9.0. Changes were made to the shell interface requiring a one-time re-initialization of conda:
conda init bash
After running the above command, you will need to restart your shell for the changes to take effect.
See: https://github.com/conda/conda/issues/11885
I recently got a new Mac with M1 processor. From the terminal I installed miniconda through home-brew. Then, I tried to create a new conda environment as usual using conda create --name my_env. Then, I try to activate the environment using conda activate my_env, but I get the following 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
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
I obey of course, and I run conda init bash and close and restart my shell. However, when I then try to activate my environment I receive the above error again (and rerunning conda init bash does not fix the problem because it simply says 'no action taken'.
Questions:
Does anyone know why I still can't run conda activate?
Is it smart to even use miniconda for Python without Rosetta?
Many thanks
Mac changed the shell from Bash to Zsh a couple years ago. So, you need to do
conda init zsh
I work with Python Poetry and need to automate the process. Specifically, everytime I need to go to a directory with
cd /work/directory
And spawn a poetry shell with
poetry shell
and again go to another directory
cd /other/directory
to finish the work.
I would love to automate this with a small script like
#!/bin/bash
cd /work/directory
poetry shell
cd /other/directory
# do work
However whenever I run this script, I get stucks at poetry shell. Is there any option like bash's -c so that I can do following?
poetry shell -c "cd /other/directory && do work"
Probably you want to run a python script using the poetry virtual environment.
In that case, I think you should be using simple the command
poetry run {your_command}
For a more practical example you can run a python script as follows
poetry run python myscript.py
which will use the poetry virtual environment you already created with poetry install.
Remix from other answers.
poetry shell spawns an environment, but we can also create an environment manualy using the env use command.
# This will create a Python3.7 env
poetry env use 3.7
Why use env use vs shell?
env use is a pure method that has an output and ends the process. It also activates the environment as required.
So after env completes we can actually use #DaveR's answer and use the run command.
A chained version of this my_bash_script.sh script would be:
#!/bin/bash
# Activate / Create + activate my enviroment
poetry env use 3.7
# Run my script in the above environment
poetry run python3.7 my_script.py
Extra
env command is helpful to see what envs you have activated.
poetry env list
ran into the same problem. this was posted by someone in the poetry discord server:
I am not sure what running poetry shell exactly entails in all details. But...
Maybe it is good enough for you to just activate the virtual environment.
And if what you want to do is not interactive work on the command line, then activating the environment might not even be necessary.
Typically you would first need to identify the path to the virtual environment with something like poetry env info --path. Once you know that you can "activate it" from anywhere:
. /path/to/venv/bin/activate
Or (in most cases) you can just use any of the executables from that virtual environment directly without having to activate first:
/path/to/venv/bin/python path/to/script.py
I want to create a bash script to install a new virtual environment "ABC" in conda. But before I go ahead and run a command to create this env, I want to check if conda is already installed on the machine. If not installed, I want to install miniconda and then create the env "ABC". If conda is already installed then I would just go ahead and create the environment. (All this should happen within the same script)
I just want to know if it is possible to check the existence of conda within a bash script and then proceed with the rest of the installations?
'''
#!/bin/bash
<code_to_check_existence_of_conda_env_here ?>
//If it does not exist, I will run the below code
mkdir -p miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O
miniconda3/miniconda.sh
bash miniconda3/miniconda.sh -b -u -p ~/miniconda3
conda env update -f my_env.yml
'''
The main issue here is that Conda has multiple components. Which components are loaded depends on the how Conda is installed and what user the BASH script is executing under. I'll try describing the components and hopefully you can decide what is suitable to verify.
Conda Components
1: Conda Package
The first is a Python package conda, which is installed in the Anaconda/Miniconda/Miniforge base environment. If the base environment is activated, one could test
python -m conda
which will give something like
/path/to/python: No module named conda
if it isn't there. Otherwise, it outputs the conda entrypoint's documentation.
2: Conda Entrypoint
The entrypoint conda, which acts as a CLI, is located under the condabin directory of the Anaconda/Miniconda/Miniforge installation. When a user runs conda init, a managed section is added to their shell initialization file (.bashrc for Linux BASH), that includes code to add the condabin to PATH. This is most likely what OP wants to identify, however, running with the shebang /bin/bash will not load the .bashrc file. Instead, one should probably be using
#!/bin/bash -l
or
#!/usr/bin/env bash -l
Then the entrypoint can be located with
which conda
3: Conda Activate
Finally, Conda also includes some shell-only functions, which are defined in the aforementioned shell initialization code. This sets up a middleman shell function, also called conda (essentially an alias), which can be viewed with
type conda
This function serves to determine whether the conda (de)?activate commands are being requested, which are pure shell functions, or something that needs to be forwarded to the entrypoint.
Recommendation
Were I designing this, I would write an interactive script that checks for #2 (which conda) and if that comes up blank then prompt the user to either provide the PATH to the Conda entrypoint (maybe they installed it in a weird place or didn't run conda init) or install Miniforge.1
I also would not use the base environment to install stuff - that is a bad idea for an end-user, let alone a third-party - and instead create a new environment. I would prompt the user with a specific default environment name, but also provide an option for them to customize.
[1] Yes, Miniforge, not Miniconda. Commercial use of the Anaconda defaults channels now requires a paid license, so better to use the free Miniforge.
I'm not really good at bash scripting but I would execute a command:
pip3 freeze | grep conda
and based on the output (if exit status was an error or not) install it or directly create the environment.
run the command conda list on your machine, the output will of be different when its installed vs not installed, then run if statement on the output.
In bash, if we want to check if a software is installed or not, the following check is used.
if dpkg -l $SOFTWARE; then
<Do stuff>
ensureconda
This sounds like the intended problem that the experimental tool ensureconda was designed to solve. This, however, requires a Python installation with pip:
pip install ensureconda
Here are the command options:
$ ensureconda --help
Usage: ensureconda [OPTIONS]
Ensures that a conda/mamba is installed.
Options:
--mamba / --no-mamba search for mamba
--micromamba / --no-micromamba search for micromamba, install if not
present
--conda / --no-conda search for conda
--conda-exe / --no-conda-exe search for conda.exe / conda-standalone,
install if not present
--no-install don't install conda/mamba if no version can
be discovered
--min-conda-version VERSIONNUMBER
minimum version of conda to accept (defaults
to 4.8.2)
--min-mamba-version VERSIONNUMBER
minimum version of mamba/micromamba to
accept (defaults to 0.7.3)
--help Show this message and exit.
Type conda activate
or conda activate env_name
The first command will activate the base environment if it is already installed. The second command will do activate the user created environment if it is installed.