How to install packages from yaml file in Conda - python

I would like to have one YAML file that could serve to both create virtual environments and (most importantly) as a base for installing packages by conda into the global env. I am trying:
conda install --file ENV.yaml
But it is not working since conda expects pip-like format of the requirements. What command should I execute to install packages from my YAML file globally?

You want the conda-env command instead, specifically
conda env update -n my_env --file ENV.yaml
Read the conda env update --help for details.
If you wish to install this in the base env, then you would use
conda env update -n base --file ENV.yaml
Note that the base env isn't technically "global", but rather just the default env as well as where the conda Python package lives. All envs are isolated unless you are either using the --stack flag during activation to override the isolation or have - contra recommended practice - manually manipulated PATH to include an env.

If your conda env is already activated, use:
conda env update --file environment.yml
Or update a specific environment without activating it:
conda env update --name envname --file environment.yml

Related

How to remove anacnda environments that's not in default env directories?

I created an conda environment in the path of my choice rather than anaconda's default directory with:
~$ conda create --prefix=/data/sfy_envs/test python=3.8
After successed, the environments are visible in conda:
~$ conda info --envs
# conda environments:
#
base * /data/miniconda3
maskrcnn_sfy /data/miniconda3/envs/maskrcnn_sfy
torch16-sfy /data/miniconda3/envs/torch16-sfy
/data/sfy_envs/test
/data/sfy_envs/tf2-sfy
The last two environments are created with --prefix parameters, and have no name. I can activate them by directly refer to their path:
~$ conda activate /data/sfy_envs/test
But I cannot remove them. For example to remove test, I tried:
~$ conda remove /data/sfy_envs/test
Collecting package metadata (repodata.json): done
Solving environment: failed
PackagesNotFoundError: The following packages are missing from the target environment:
- /data/sfy_envs/test
and
~$ conda remove -p /data/sfy_envs/test
CondaValueError: no package names supplied,
try "conda remove -h" for more details
These won't work and I have on idea why.
Or could I just delete the environment directory manually, and remove their paths from the file .conda/environments.txt? I'm not sure if it's a safe treatment.
Use
conda env remove --prefix /data/sfy_envs/test
or
conda remove --prefix /data/sfy_envs/test --all

How do I create a conda env from a yml with no default packages?

I want to describe all my deps in a yml file:
I can do this and run this command: conda env create -f environment.yml
This installs a bunch of extra rubbish I don't want.
But I can run this command to create a minimal conda env: conda create --name test-layers python --no-default-packages
OK so now I want to use my yml config.
This doesn't work: conda create --name myenv -f environment.yml --no-default-packages --yes
Error: PackagesNotFoundError: The following packages are not available from current channels: -environment.yml
And this doesn't work: conda env create -f environment.yml --no-default-packages
Error: unrecognized arguments: --no-default-packages
How do I use a yml file and also not install a bunch of default packages?
Edit
I feel like I'm missing something because the tooling can't be this obtuse and stupid. I thought I would try to create the env first and then update from a file to see if at least that worked:
conda create --name myenv python --no-default-packages --yes
conda env update --name myenv --file environment.yml
And now my env has all those default packages I wanted to avoid! Am I seriously going to have to wrap this in a script that parses my environment.yaml and runs a command to install each dep and pip package myself?
Did you try using --no-deps? as in:
conda create --name myenv -f environment.yml --no-deps --yes

CondaValueError: The target prefix is the base prefix. Aborting

I have the following conda environment file environment.yml:
name: testproject
channels:
- defaults
dependencies:
- python=3.7
prefix: /opt/projects/testproject
Before creating the environment, only the base environment exists:
(base) me#mymachine:/opt/projects/testproject$ conda env list
# conda environments:
#
base * /opt/anaconda/anaconda3
When trying to create the environment, I get the following error:
(base) me#mymachine:/opt/projects/testproject$ conda create -f environment.yml
CondaValueError: The target prefix is the base prefix. Aborting.
What does this error mean?
You need to use
conda env create -f environment.yml
Notice the extra env after conda and before create.
For more information check the documentation.
Very tricky, see the difference between the two:
conda create –-name my_env
and
conda create --name my_env
The first dash before name is slightly different (– instead of -). It takes me 15 mins to notice.
You can use:
conda create --name nameOfEnv
I have had the same issue even with correct command syntax, right after the anaconda installation. The solution was to make the base environment not be activated on startup:
conda config --set auto_activate_base false
Then restart you terminal.
After that I've bean able to create my first conda environment.

Conda: Creating a virtual environment

I'm trying to create a virtual environment. I've followed steps from both Conda and Medium.
Everything works fine until I need to source the new environment:
conda info -e
# conda environments:
#
base * /Users/fwrenn/anaconda3
test_env /Users/fwrenn/anaconda3/envs/test_env
source ~/anaconda3/bin/activate test_env
_CONDA_ROOT=/Users/fwrenn/anaconda3: Command not found.
Badly placed ()'s.
I can't figure out the problem. Searching on here has solutions that say adding lines to your bash_profile file, but I don't work in Bash, only C shell (csh). It looks like it's unable to build the directory path in activate.
My particulars:
OS X
Output of python --version:
Python 3.6.3 :: Anaconda custom (64-bit)
Output of conda --version:
conda 4.4.7
I am not sure what causes the problem in your case, but code below works for me without any issues (OS X, the same version of Conda as yours).
Creation of the environment
conda create -n test_env python=3.6.3 anaconda
Some explanation of the documentation of conda create is not clear:
-n test_env sets name of the environment to test_env
python=3.6.3 anaconda says that you want to use python in version 3.6.3 in this environment (exactly the one you have, and you can use a different one if you need it) and package anaconda. You can put all the things you need there, separated with spaces, e.g., sqlite matplotlib requests and specify their versions the same way as for python.
Activation
conda activate test_env
Deactivation
conda deactivate
Getting rid of it
conda remove -n test_env --all
Check if Conda is installed
conda -V
Check if Conda is up to date
conda update conda
Create a virtual environment
conda create -n yourenvname python=x.x anaconda
Activate your virtual environment
source activate yourenvname
Install additional Python packages to a virtual environment
conda install -n yourenvname [package]
Deactivate your virtual environment
source deactivate
Delete the virtual environment
conda remove -n yourenvname --all
I was able to solve my problem. Executing the source activate test_env command wasn't picking up my .bash_profile, and I normally work in tcsh. Simply starting a subprocess in Bash was enough to get activate working. I guess I assumed, incorrectly, that the activate command would start a child process in Bash and use Bash environment variables.
> conda info -e
> # conda environments:
> #
> base * ~/anaconda3
> test_env ~/anaconda3/envs/test_env
> bash
~$ source ~/anaconda3/bin/activate test_env
(test_env) ~$
(test_env) ~$ conda info -e
# conda environments:
#
test_env * ~/anaconda3/envs/test_env
root ~/anaconda3

Can conda automatically activate the env from an env environment.yml?

This my environment.yml:
name: my-env-name
dependencies:
- blah
To create this env I can cd to the dir with the yml in it and do this:
conda env create
Conda's smart enough to look in the yml and see the env name.
But to activate it why do I have to do this:
source activate my-env-name
Is there a switch to have conda just activate the env name from the environment.yml?
It's impossible. Workaround: direnv with anaconda layout

Categories

Resources