Create virtual environments for python with conda (Anaconda)



A virtual environment is a named, isolated, working copy of Python that that maintains its own files, directories, and paths so that you can work with specific versions of libraries or Python itself without affecting other Python projects. Virtual environments make it easy to cleanly separate different projects and avoid problems with different dependencies and version requirements across components. The conda command is the preferred interface for managing installations and virtual environments with the Anaconda Python distribution. If you have a vanilla Python installation or other Python distribution see virtualenv.

Prerequisites


Check conda is installed and in your PATH

Enter conda -V into the terminal command line and press Enter.

$ conda -V
conda 4.5.12

Check conda is up to date

In the terminal enter the command line:

$ conda update conda

Create a virtual environment for your project

a) To create an environment:

$ conda create -n your_env_name
where your_env_name is the name you want to call your environment

b) To create an environment with a specific version of Python:

$ conda create -n your_env_name python=x.y
replace x.y with the Python version you wish to use. (To see a list of available python versions first, type conda search "^python$" and press Enter.)

c) To create an environment with a specific package:

$ conda create -n your_env_name scipy
Or:

$ conda create -n your_env_name python
$ conda install -n your_env_name scipy
d) To create an environment with a specific version of Python and multiple packages:

$ conda create -n your_env_name python=3.4 scipy=0.15.0 astroid babel

Tip
Install all the programs that you want in this environment at the same time. Installing 1 program at a time can lead to dependency conflicts.

Activating an environment

Activating environments is essential to making the software in the environments work well. Activation entails two primary functions: adding entries to PATH for the environment, and running any activation scripts that the environment may contain. These activation scripts are how packages can set arbitrary environment variables that may be necessary for their operation.

To activate an environment: 

$ conda activate your_env_name 

Note
Replace your_env_name with the environment name or directory path.

Deactivating an environment

To deactivate an environment, type:

$ conda deactivate
Conda removes the path name for the currently active environment from your system command.

Note
To simply return to the base environment, it's better to call conda activate with no environment specified, rather than to try to deactivate. If you run conda deactivate from your base environment, you may lose the ability to run conda at all. Don't worry, that's local to this shell - you can start a new one.

Viewing a list of your environments

You can list all discoverable environments with:

$ conda info --envs
Or:


$ conda env list
In the environments list that displays, your current environment is highlighted with an asterisk (*).

Viewing a list of the packages in an environment

To see a list of all packages installed in a specific environment:

If the environment is not activated, in your terminal window or an Anaconda Prompt, run:

$ conda list -n your_env_name

If the environment is activated, in your terminal window or an Anaconda Prompt, run:

$ conda list

To see if a specific package is installed in an environment, in your terminal window or an Anaconda Prompt, run:

$ conda list -n your_env_name scipy

Using pip in an environment

Some packages are not available and can not install by command conda install, so we can install them by pip in an environment.

To use pip in your environment, in your terminal window or an Anaconda Prompt, run:

$ conda install -n your_env_name pip
$ conda activate your_env_name
$ pip <pip_subcommand>

Removing an environment

To remove an environment, in your terminal window or an Anaconda Prompt, run:

$ conda remove --name myenv --all
You may instead use:

$ conda env remove --name myenv

To verify that the environment was removed, in your terminal window or an Anaconda Prompt, run:

$ conda info --envs

Cloning an environment

Use the terminal or an Anaconda Prompt for the following steps:

You can make an exact copy of an environment by creating a clone of it:

$ conda create --name my_clone_env_name --clone your_env_name

Note
Replace my_clone_env_name with the name of the new environment. Replace your_env_name with the name of the existing environment that you want to copy.

Good Luck!

Post a Comment

0 Comments