manage conda and python.

Install Miniconda

Linux

download the latest shell script

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
wget https://repo.anaconda.com/miniconda/Miniconda3-py310_23.1.0-1-Linux-x86_64.sh

Make the miniconda installation script executable

chmod +x Miniconda3-latest-Linux-x86_64.sh

Run miniconda installation script

bash ./Miniconda3-latest-Linux-x86_64.sh -p /opt/miniconda -b
  • -p /opt/miniconda: This option specifies the installation path for Miniconda. In this case, it is set to “/opt/miniconda”, which means Miniconda will be installed in the “/opt/miniconda” directory.
  • -b This option stands for “batch mode” and instructs the installer to run in a non-interactive mode, skipping prompts and assuming default options for any questions asked during the installation process. Load conda environment when login
export PATH=$PATH:/opt/miniconda/bin
conda init

Windows

remember add ~/miniconda and ~/miniconda\Scripts to PATH.

MacOS

  1. remember download .sh installer when use m1/m2 chip, pkg installer may not work on m1/m2 mac.
  2. after installed it, please add conda execute script to ~/.zprofile
$ vim ~/.zprofile

# conda
export PATH=/Users/frederick/miniconda3/bin:$PATH

$ conda init zsh
  1. apply it to zsh
/(your conda installation path)/bin/conda init zsh

Basic Usage for Miniconda

manage environment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# list all virtual environments
conda env list

# create a New environment
conda create -n myfirstenv python=3.10.10

# delete the environment
conda env remove --name ENV_NAME --all

# activate the environment
conda activate myenv

# deactivate the environment
conda deactivate

modify channels

1
2
3
4
5
6
7
8
# show channels
conda config --show channels
# add channels
conda config --add channels http://192.168.1.113:8081/repository/conda-proxy/main
# remove all channels
conda config --remove-key channels
# remove specific channels
conda config --remove channels defaults

linux file localtion on /home/frederick/.condarc windows file location on C:\Users\frederick\.condarc

tsinghua mirrors

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

USTC mirrors

channels:
  - http://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  - http://mirrors.ustc.edu.cn/anaconda/pkgs/free/
  - http://mirrors.ustc.edu.cn/anaconda/pkgs/main/
auto_activate_base: false
show_channel_urls: true

after saved, use conda clean -i remove index cache.

manage package

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Searching for packages
conda search "^python$"
## To see if a specific package, such as SciPy, is available for installation from Anaconda.org:
conda search --override-channels --channel defaults scipy
# If you do not specify the environment name, which in this example is done by --name myenv, the package installs into the current environment:
conda install scipy
## To install a specific package such as SciPy into an existing environment "myenv":
conda install --name myenv scipy
## To install a specific version of a package such as SciPy:
conda install scipy=0.15.0
# To update a specific package:
conda update biopython
# To remove a package such as SciPy in an environment such as myenv:
conda remove -n myenv scipy

Create virtual environment (venv) using Python

Navigate to the directory where you want to create the virtual environment.Once you are in the desired directory, run the following command to create a new virtual environment:

python3 -m venv myenv

To activate the virtual environment, run the appropriate command based on your operating system:

source myenv/bin/activate

To exit the virtual environment when you’re done working with it, simply run the following command:

deactivate

install package for venv

Once the virtual environment is activated, you can install Python packages using pip without affecting your system-wide Python installation. For example:

pip install package_name

The packages you install will be isolated within the virtual environment and won’t affect other projects or the system.

Once you are in the correct directory, run the following command to install the packages from the requirements.txt file:

pip install -r requirements.txt

This command tells pip to install all the packages listed in the requirements.txt file.

Note that the requirements.txt file should be properly formatted, with each package listed on a separate line. For example:

# comment here
scikit-learn==1.1.3  # comment here
ipython>=8.0

change mirror for venv

Run the following command to open the pip.ini (Windows) or pip.conf (macOS and Linux) file for editing:

vim $VIRTUAL_ENV/pip.conf

here the example configuration

[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

after saved, use pip config list -v to validate the changed mirror.

Jupyter Notebook use venv or conda environment

Activate your virtual environment. If you haven’t activated it yet, refer to the previous answer on how to activate a virtual environment.

Install the ipykernel package within your virtual environment. This package allows Jupyter Notebook to recognize your virtual environment as a kernel.

pip install ipykernel
# or using conda
conda install ipykernel

Register the virtual environment as a kernel by running the following command:

python -m ipykernel install --user --name=myenv

Delete don’t want kernel.

# List the installed kernels
jupyter kernelspec list
# you can delete it using the following command:
jupyter kernelspec uninstall <kernel_name>

Launch Jupyter Notebook by running the command:

$ nohup jupyter notebook > ./jupyter.log &

# or modify config before run it
$ jupyter notebook --generate-config