Conda: Creating a virtual environment
Asked Answered
U

4

61

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
    
Uvarovite answered 9/1, 2018 at 18:55 Comment(0)
U
4

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
Uvarovite answered 10/1, 2018 at 15:30 Comment(0)
A
125

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
Alage answered 10/1, 2018 at 0:46 Comment(4)
Thanks. Your answer helped, and so did conda.io/docs/user-guide/tasks/manage-environments.htmlElke
Semantics question: The authour asks about "virtual environment", the answer is regarding an "environment". Is there a difference between the two? I had the impression that a VIRTUAL environment is more light-weight, but maybe I am mistakenBiondo
In general yes, there are differences between virtual and non-virtual environment. In context of this question, no - when you create an environment using conda create, it's a virtual one.Alage
Documentation link was dead. I found thisWernsman
S
19

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
Spaceman answered 11/12, 2019 at 7:47 Comment(0)
U
4

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
Uvarovite answered 10/1, 2018 at 15:30 Comment(0)
A
0

Just above the thing @machnic mentioned, a noteworthy thing I need to mention is if the Python version is quite backdated, you may not get it installed.

For instance, I was trying installing 3.6.3 as per @machnic, in this Oct 2023, on my Macbook M1. However, it wasn't installed. After that, when I just shifted it to 3.8, it just got installed.

So, please make sure your Python version is not old. I know it may not be applicable for all cases but this may help.

Astrograph answered 12/10, 2023 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.