Month: December 2018

(I use Mac and Linux OS, so the commands here assume that you use Mac and Linux OS. If you are using Windows, most of the conda commands should be the same, but some command might be slightly different. For example, the one I am aware of is that the command for activating and deactivating conda environment is a bit different, you do not need to add “source” in the command like Mac and Linux OS do).

(To use conda, you need to have either Miniconda or Anaconda installed on your machine. Check out here for installing Miniconda on Mac, and here for installing Miniconda on CentOS7/RedHat7. Check here if you would like to know the difference between Miniconda and Anaconda.)

The first part of this post introduces common conda commands, and the second part of this post provides some package installation ( e.g., OpenCV, scikit-image, jupyter notebook etc.) using conda.

1. Common conda commands

(1) Check whether conda is installed / check conda version info

In your terminal, type in the following command

$ conda -V
#if you see something like below it means conda is installed, and it provides its version info.
conda 4.5.11

Pressyto proceed. This will install the Python version (and all the associated anaconda packaged libraries if you installed conda via Anaconda) at “path_to_your_anaconda_location/anaconda/envs/yourenvname” or at “path_to_your_miniconda_location/miniconda/envs/yourenvname”

(4) Activate your virtual environment

Once we created a virtual environment using conda, before we start to using it, we need to activate it first each time we need to use the virtual environment.

To activate or switch into your virtual environment, simply type the following where yourenvname is the name you gave to your environement when creating.

$ source activate yourenvname

Activating a conda environment modifies the PATH and shell variables to point to the specific isolated Python you created. Note that the command prompt will change to indicate which conda environemnt you are currently in by prepending (yourenvname).

If you do not remember your virtualenv or do not want to type it, you can use the following command to see a list of all your environments,

$ conda info -e

(5) Install (additional) Python packages to a virtual environment

To install packages only to your virtual environment (not system wide), enter the following command

$ conda install -n yourenvnamepackage-name
# yourenvname is the name of your environment, and package-name is the name of the package you would like to install.
# Note that if not specify “-n yourenvname” will install the package to the root Python installation.

Or you can simply first activate and into the virtual environment you would like to install packages [see the command above in (4)], and then use the following command

Each time once we finish working in your virtual environment created using conda, we will need to deactivate the virtual environment to exit from it.

To end a session in the current virtual environment, enter the following command .

$ source deactivate
# Note that we do not need to specify the envname - whichever is currently active will be deactivated, and the PATH and shell variables will return to normal.

(7) Delete a virtual environment

When we do not need a virtual environment created by conda any more, we can simply remove it by the following command.

conda remove -n yourenvname -all
# yourenvname is the name of the environment you would like to delete.
# (You may instead use $ conda env remove -n myenv.)

To verify that the environment was removed, in your Terminal window or an conda Prompt, run the following

$ condainfo -e

The environments list that displays should not show the removed environment.

(8) Cloning a conda virtual environment

To make an exact copy of an environment by creating a clone of it, using the following command,

$ condacreate--namemyclone--clonemyenv
# NOTE: replace myclone with the name of the new environment. Replace myenv with the name of the existing environment that you want to copy.
# see the following for an example of cloning py35 and naming the new copy as py35-2
$ conda create --clone py35 --name py35-2

To verify that the copy was made:

condainfo-e

In the environments list that displays, you should see both the source environment and the new copy.

(9) Viewing a list of your environments

To see a list of all of your conda virtual environments, in your Terminal or in one of your conda virtual environment, run one of the following commands:

$ condainfo -e
OR
$ conda info --envs
OR
$ conda env list

You will see a list of all your conda environments, and the active environment is shown with *.

(10) Viewing a list of the packages in a conda environment

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

If the environment is not activated, in your Terminal window or a conda prompt, run the following:

$ conda list -n myenv

If the environment is activated, in your Terminal window or a conda prompt, run the following:

$ conda list

To see whether a specific package is installed in a conda environment,

If the environment is not activated, in your Terminal window or a conda prompt, run the following:

If the environment is activated, in your Terminal window or a conda prompt, run the following:

$ conda list package-name
# for example, the following command will list the opencv versions installed in the current active conda environment you are in
$ conda list opencv

2. Package installation using conda

Before installing packages using conda, make sure to first create a conda virtual environment [see the command at (3) in section 1 above in this post] and then activate and into the environment you would like to install the packages into [see the command for (4) in section 1 above in this post].

The command above will install (by default) the latest version of opencv available in the conda repository. if you would like to specifiy which version of openCV to install, you can first use the following comamnd to check OpenCV versions available.

$ conda search "^openCV$"# you should see a list of openCV versions.

Then you could use the following command to install the version of OpenCV you would like to install,

$ conda install -c conda-forge opencv=x.x
# for example, the following command will install openCV 3..4.1, instead of the current lastest version 3.4.2. Note that is opencv==3.4.1, not opencv=3.4, if opencv=3.4, it will install openCV3.4.2 (the latest version in 3.4 series).
$ conda install -c conda-forge opencv==3.4.1

A file called Miniconda3-latest-MacOSX-x86_64.sh will be shown in your Downloads folder.

Step 2: Open a Terminalwindow.

(If you don’t know how to open a terminal window, through lauchpad type in Terminal, you will see the application.)

In the terminal window, type in

$ cd Downloads

Step 3: run the bash “shell” script to install Miniconda

In the terminal window, type in the following.

$ bash Miniconda3-latest-MacOSX-x86_64.sh

Scroll through the license (press the Space bar or Enter to move through quickly), type ‘yes’ to approve the terms, and then accept all the installation defaults.

Step 4: Close the Terminal window, and open a new Terminal window.

in the newly opened Terminal window.

Type the following:

$ conda -V

If you see something like the following, it means you have successfully installed conda via miniconda on your Mac.

conda 4.5.11

Step 5: Uninstalling Miniconda

To uninstall Python Anconda/Miniconda, we just simply remove the installation folder and remove the environment variables set in the hidden file .bash_profile in your home directory. For my installation, it will be just like this.

If you do not know where the hidden .bash_profile is located and how to edit it, see below for detailed instructions.

(1) Open a new terminal and go to your home directory. You can do this by using the command below.

$ cd

(2) use the following command to list all files, including hidden files in your home directory.

$ ls -a
# you should see there is a file called .bash_profile.

(3) Use the cat command to see the contents of the hidden file .bash_profile. Type the following command into your terminal.

$ cat .bash_profile

You will see something like the following (depends on what you installed, if you installed Miniconda3, you will only see the first two lines. If you installed Anaconda3, you will see the bottom two lines.

This post briefly introduces which to choose among Anaconda, Miniconda, and Virtualenv.

If you have used pip and virtualenv in the past, you can use conda to perform all of the same operations. Pip is a package manager, and virtualenv is an environment manager; and conda is both.

Specifically, conda is a packaging tool and installer that aims to do more than what pip does; it handles library dependencies outside of the Python packages as well as the Python packages themselves. Conda also creates a virtual environment, like virtualenvdoes.

Both Anaconda and Miniconda uses Conda as the package manager. The difference among Anaconda and Miniconda is that Miniconda only comes the package management system. So when you install it, there is just the management system and not coming with a bundle of pre-installed packages like Anaconda does. Once Conda is installed, you can then install whatever package you need from scratch along with any desired version of Python.

Choose Anaconda if you:

Are new to conda or Python

Prefer having Python and 720+ open source certified packages automatically installed at once

Have the time and disk space (a few minutes and 3 GB), and/or

Don’t want to install each of the packages you want to use individually.

Choose Miniconda if you:

Know what package(s) you need to install

Do not have time or disk space (about 3 GB) to install over 720+ packages (many of the packages are never used and could be easily installed when needed), and/or

Just want fast access to Python and the conda commands, and prefer to sorting out the other packages later.

Choose Virtualenv only when you have sudo access to the machine you are working on. It is much easier to setup conda rather than virtualenv for a regular (i.e., non sudo/root) user on a linux/Mac machine.

I use Miniconda myself (because it is much more light weight than Anaconda) when I need to setup python programming environment and when I do not have sudo privilege, and I use Virtualenv when I have sudo access on the machine.