Anaconda Virtual Environment Management

Posted on 2020-04-29  4 Views


Tip: At any time you can get the full documentation of the command by following it with --help. For example, you can learn the update command of conda with the following command.

conda update --help

1. Managing Conda:

Conda is both a package manager and an environment manager. You definitely know the package manager, which can help you discover and view packages. But if we want to install a package, but the package only supports a different version of Python than we are currently using. With just a few lines of commands, you can set up an environment that can run a different version of Python. , that's the power of Conda Environment Manager.
Tip: Whether you use Linux, OS X or Windows command-line tools, the conda command is the same in your command line terminal, unless otherwise noted.

Check that conda is already installed.

To make sure you have installed conda in the right place, let's check if you have successfully installed Anaconda. In your command line terminal window, enter the following code:

conda --version

Conda will return the version of your Anaconda software installed.
Tip: If you see an error message, check that you selected to press Install only for the current user during installation and that you are operating with the same account. Make sure you are logged in with the same account and reopen the command line terminal window after installation.

Upgrade the current version of Conda

Next, let's upgrade conda by using the update command like this:

conda update conda

Conda will compare the old and new versions and tell you which version of Conda can be installed. It will also notify you of other packages being upgraded at the same time as this upgrade.
If a new version of Conda is available, it will prompt you to enter y to upgrade.

proceed ([y]/n)? y

Once conda is updated to the latest version, we will move on to the next topic.

2. Manage the environment.

Now we show Conda's environment operations by creating some environments and then move them.

Create and activate an environment

Use the conda create command, followed by whatever name you wish to call it:

conda create --name snowflake biopython

This command will create a new environment for the biopython package in /envs/snowflakes
tip: many of the commonly used command options that follow -- can be abbreviated as a short line plus the command initial. So the --name option and -n do the same thing. Look at a large number of abbreviations by conda -h or conda --help.

Activate this new environment

Linux,OS X: source activate snowflakes
Windows:activate snowflake`

Tip: The new development environment will be installed by default in the envs file directory in your conda directory. You can specify a different path; Go to conda create -h to learn more.
Tip: If we don't specify the version of Python to install, Donda will install the version of Python that we originally installed when Conda was installed.

Create a second environment

This time let's create and name a new environment, then install another version of Python and two packages, Astroid and Babel.

conda create -n bunnies python=3 Astroid Babel

This will create a second python3-based new environment containing Astroid and Babel packages called bunnies, in the /envs/bunnies folder.
Tip
: Install all the packages you want to run in this environment at the same time, and install all the packages you want at the same time as you create the environment, and installing them later in turn may cause dependency problems (it seems that I don't know how to translate this term).
Tip: You can append more conditions after the conda create command, type conda create –h to see more details.

List all environments

Now let's check the environment you have installed so far and use the conda environment info command to view it:

conda info --envs

You will see a list of environments below:

conda environments:

 snowflakes          * /home/username/miniconda/envs/snowflakes
 
bunnies               /home/username/miniconda/envs/bunnies
 
root                  /home/username/miniconda

Confirm the current environment

What environment are you in now? Snowflakes or bunnies? To determine it, enter the following code:

conda info -envis

Conda will display a list of all environments, with the current environment displayed in a parenthesis.

(snowflakes)  

Note: Conda sometimes puts an * sign in front of the currently active environment.

Switch to another environment (activate/deactivate)

To switch to another environment, type the following command and the name of the desired environment.

Linux,OS X: source activate snowflakes
Windows:activate snowflakes

If you want to switch from the path to your current working environment to the system root, type:

Linux,OS X: source deactivate
Windows: deactivate

When the environment is no longer active, it will no longer be displayed ahead of time.

Copy an environment

Duplicate an environment by cloning. Here, a copy called flowers will be created by cloning Snowfllakes.

conda create -n flowers --clone snowflakes

Check the environment
by conda info –-envs and you should now see a list of environments: flowers, bunnies, and snowflakes.

Delete an environment

If you don't want the environment called flowers, remove it as follows:

conda remove -n flowers --all

To make sure that the environment called flowers has been removed, enter the following command:

conda info -e

Flowers is no longer in your environment list, so we know it's removed.

Learn more about the environment

If you want to learn more about conda's command, follow the command with -h

conda remove -h

3. Manage Python

Conda's management of Python is similar to that of other packages, so it is easy to manage and upgrade multiple installations.

Check the python version

First let's check which version of Python can be installed:

conda search --full --name python

You can use Conda Search Python to see all packages with "Python" in their names, or add the --full --name command option to list packages that exactly match "Python".

Install a different version of Python

Now let's assume you need python3 to compile the program, but you don't want to overwrite your python 2.7 to upgrade, you can create and activate an environment called snakes and install the latest version of python3 with the following command:

conda create -n snakes python=3
· Linux,OS X:source activate snakes
· Windows: activate snakes

Tip: It's wise to give the environment a nice name, such as "Python3", but it's not fun.

Make sure the environment was added successfully

To ensure that the snakes environment is installed, type the following command:

conda info -e

conda displays a list of environments, and the currently active environment is enclosed in parentheses (snakes).

Check the Python version in the new environment

Make sure the snakes environment is running python3:

python --version

Use different versions of Python

In order to use a different version of Python, you can switch the environment, simply activate it, let's see how to return to the default 2.7

· Linux,OS X: source activate snowflakes
· Windows:activate snowflakes

Check the python version:

Make sure that the SnowFlakes environment is still running the same version of Python that you installed when you installed Conda.

python --version

Log off the environment

When you're done working in the Snowflakes environment, unregister the environment and convert your path to the previous state:

· Linux,OS X:source deactivate
· Windows:deactivate

4. management pack

Now let's demo the package. We've installed some packages (Astroid, Babel, and some specific versions of python) when we create a new environment. We check which packages we have installed, check which ones are available, look for a specific package and install it. Next, we find and install some specified packages in the Anconda.org repository, use conda to complete more pip-achievable installations, and install a commercial package.

To view a list of packages and their versions in that environment:

Use this command to see which version of Python or other programs is installed in the environment, or to make sure that certain packages have been installed or removed. In your terminal window, enter:

conda list

Use the conda command to view a list of available packages

A list of packages that can be installed by conda, categorized by Python version, can be obtained from this address:
http://docs.continuum.io/anaconda/pkg-docs.html

Find a package

First let's check if the package we need can be installed via conda:

conda search beautifulsoup4

It showcases this package so we know it's available.

Install a new package

We will install this Beautiful Soup package in the current environment, using the conda command as follows;
conda install --name bunnies beautifulsoup4
Tip: You must tell conda the name of the environment you want to install (-n bunies) or it will be installed into the current environment.
Now activate the bunnies environment and use the conda list to show which programs are installed.

· Linux,OS X:source activate bunnies
· Windows:activate bunnies
All platforms:
conda list

Install a package from the Anaconda.org

If a package cannot be installed using conda, we will look for it next on the Anaconda.org website. Anaconda.org provides package management services to public and private package repositories. Anaconda.org is a continuous analytical product.
Tip: Registration is not mandatory when you download something Anaconda.org.
In order to download from Anaconda.org to the current environment, we need to do so by specifying the Anaconda.org as a specific channel, by entering the full path to this package.
In your browser, go to http://anaconda.org website. We look for a package called "bottleneck", so type "bottleneck" in the search box called "Search Anaconda Cloud" in the upper left corner and click the search button.
There will be more than a dozen versions of the Bottleneck Pack available on Anaconda.org, but we want the one that is most frequently downloaded. So you can sort by the number of downloads, by clicking on the Download bar.
Click on the package name to select the most frequently downloaded package. It links to Anaconda.org details page showing the specific commands downloaded:

conda install --channel https://conda .anaconda.ort/pandas bottleneck

Check the downloaded package

conda list

Install the package via the pip command

For packages that cannot be installed by conda or obtained from Anaconda.org, we can usually install packages with pip (short for "pip install packages").
Tip: pip is just a package manager, so it can't manage the environment for you. pip can't even upgrade Python because it doesn't treat Python as a package like Conda. However, it can install packages that Conda can't install, and Vice Versa (which won't be translated here). pip and conda are integrated into Anaconda or miniconda.

We activate the environment in which we want to place the program and install a program called "See" through pip.

· Linux,OS X: source activate bunnies
· Windows:activate bunnies
All platforms:
pip install see

Check pip installation

Check if the See is installed:

conda list

Install the commercial package

Installing commercial packages is unusual with the process of installing other packages. As an example, let's install and remove an updated commercial package for free trial IOPro that can speed up your python processing speed:

conda install iopro

Tip: Except for academic use, this version expires after a trial period of 30 days

You can now install and check any package you want to install with conda, whether using the conda command, downloading from Anaconda.org, or installing with pip, whether open source software or commercial packages.

5. Remove packages, environments, or condas

If you will. Let's conclude this test guide by removing one or more test packages, environments, and conda.

Remove the package

Let's say you decide not to use the commercial package IOPro anymore. You can remove it in the bunnies environment.

conda remove -n bunnies iopro

Verify that the package has been removed

Use the conda list command to confirm that IOPro has been removed

conda list

Remove the environment

We don't need the snakes environment anymore, so enter the following command:
conda remove -n snakes --all

Confirm that the environment is removed

To confirm that the snakes environment has been removed, enter the following command:

 conda info --envis

snakes no longer appear in the environment list, so we know it's been removed

Delete conda

  • Linux, OS X:
    Remove the Anaconda or Miniconda installation folder

rm -rf ~/miniconda OR  rm -rf ~/anaconda

Author: NorthPenguin
Link: https://www.jianshu.com/p/d2e15200ee9b
Source: Jianshu
copyright belongs to the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.