Setting up Pyhton Environment Based on Anaconda

Tensorflow, PyTorch, Meep and more...

1. Anaconda Installation

1.1 Download

Download the installl package from mirrors.

I prefer miniconda because it has less pre-installed packages. We’d better use all packages through manual installation rather than the pre-installed ones.

To start installation on Linux, tpye

$ bash Miniconda-latest-Linux-x86_64.sh

1.2 Source mirrors

To increase the download speed, refer this help and add mirrors to the file .condarc in the user folder.

2. Anaconda Environment Initialization

  • On Linux, if the init script was not added to .bashrc during installation, type
$ eval "$(~/miniconda3/bin/conda shell.bash hook)"
  • On Windows, start anaconda from Start Menu.

(base) will show at the beginning of command prompt.

3. Python Package Installation

Each python project (Tensorflow, PyTorch, and etc) should be installed independently. This can be easily managed by conda environment.

There are 2 commonly used commands.

  • conda create

Create a new environment with some packages.

  • conda install

If more packages are needed in one project, use this command.

In general, pip is used only when a package is not provided in anaconda.

3.1 Tensorflow

conda create -n tf tensorflow

Use tensorflow-gpu to support GPU.

3.2 PyTorch

conda create -n torch pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch-lts

See official website for more options. Do not just copy the command which uses conda install and wrongly installs PyTorch to base environment rather than an independent one. Replace it with conda create -n xxx.

3.3 Meep

conda create -n meep -c conda-forge pymeep=*=mpi_mpich_*

4. Using Python Environment

conda activate env_foo

where env_foo is the example name of anaconda environment and it will replace the base at the beginning of command prompt.

If you need more packages, just use conda install to add them.

5. Clean installation cache

After installation, use the command below to remove the downloaded installation packages and unnecessary packages.

conda clean --all

6. Other useful anaconda commands

  • Exit current environment
conda deactivate
  • List all environments
conda env list
  • List installed packages in the current environment
conda list
  • Install a package in the current environment
conda install package_name
  • Delete one environment
conda env remove -n env_foo