python package management-conda(miniconda)

configuration

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - http://mirrors.aliyun.com/anaconda/pkgs/main
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
auto_activate_base: false
envs_dirs:
  -F:\ProgramData\miniconda3\envs
pkgs_dirs:
  - F:\ProgramData\miniconda3\pkgs

Installation configuration

Download

Anaconda official website

Anaconda download mirror

Miniconda

Installation/Configuration Environment Variables

There can be no spaces in the installation directory

Anaconda can be checked and added to the environment variable during installation.

The installation of the new version of Miniconda will no longer prompt to add environment variables, and you need to add environment variables manually.

Take the installation in F:\ProgramData\miniconda3 as an example, add the following paths to the path variable

F:\ProgramData\miniconda3
D:\ProgramData\miniconda3\Scripts
D:\ProgramData\miniconda3\Library\bin
D:\ProgramData\miniconda3\Library\mingw-w64\bin

In order not to affect the original python environment, it is recommended to put the original py environment configuration variables above, and conda configuration under the system python

Start and run the command box, enter conda, if the result is displayed as shown in the figure below, it means that the Anaconda environment variable configuration is successful

image-20230110225549622

image-20230110225629600

windowscomputer modify PowerShellrunning policy

In PowerShell for security reasons, the default policy does not allow script execution. If you don’t modify it, you can only run the script in cmd.

Get-ExecutionPolicy indicates the permission to execute PowerShell scripts in the current environment, which has 4 values

  • Restricted, the default setting, does not allow any scripts to run
  • AllSigned, only run trusted scripts
  • RemoteSigned runs local scripts whether they are trusted or not;
  • Unrestricted, allowing all scripts to run, even untrusted ones;

In general, set the level to RemoteSigned to facilitate script debugging and running.

Run the command in the PowerShell window as an administrator and enter:

Set-ExecutionPolicy unrestricted

Conda command line initialization

conda init powershell

After conda is installed, when running conda activate xxx to switch environments, an error may be reported

Administrators are required to run cmd settings, otherwise some do not have permission to set

Conda needs to initialize the current command line environment before it can be used. Please initialize according to the command line tool you use. Enter commands such as conda init powershell or conda init bash. After inputting, restart the command line.

After the initialization is complete, under normal circumstances, the current conda environment will be displayed before each command

image-20230111003326992

Cancel the base virtual environment automatically loaded by the terminal

After the Conda command line is initialized, the conda (base) environment will be automatically loaded every time you enter the command line, causing the system python environment to fail to run

conda config --set auto_activate_base false

After execution, a sentence auto_activate_base: false will be generated at the end of the C:\Users\Hi\.condarc configuration file

At the same time, the base environment will not be loaded every time the command is opened

image-20230111004503095

Modify directory permissions

By default, only the administrator has permission to read and write the envs directory of miniconda3\envs during a new installation, and the permissions need to be modified to facilitate all users to read and write

Mirror configuration

Modify the image of python pip in all environments

# set mirroring
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

There are two ways to modify the mirror source

Direct command number execution

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
  • When setting the source address to be displayed during installation, it is convenient to know which source the package comes from during installation

    conda config --set show_channel_urls yes
    

Edit the C:\Users\Hi\.condarc file, .condarc may be hidden.

Windows users cannot directly create a file named .condarc, they can execute conda config --set show_channel_urls yes first to generate the file and then modify it.

  • Tsinghua Mirror
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
  • Ali Cloud

    channels:
      - defaults
    show_channel_urls: true
    default_channels:
      - http://mirrors.aliyun.com/anaconda/pkgs/main
      - http://mirrors.aliyun.com/anaconda/pkgs/r
      - http://mirrors.aliyun.com/anaconda/pkgs/msys2
    custom_channels:
      conda-forge: http://mirrors.aliyun.com/anaconda/cloud
      msys2: http://mirrors.aliyun.com/anaconda/cloud
      bioconda: http://mirrors.aliyun.com/anaconda/cloud
      menpo: http://mirrors.aliyun.com/anaconda/cloud
      pytorch: http://mirrors.aliyun.com/anaconda/cloud
      simpleitk: http://mirrors.aliyun.com/anaconda/cloud
    

Modify the creation environment location

The envs path under the anaconda installation directory was not found during creation, and the newly created environment is in C:\Users\xxx.conda\envs\

Pay attention to change the dividing line of the path to double slashes

There is a .condarc file under C:\Users\username, open it, and add the following content at the end of it:

envs_dirs:
  -F:\ProgramData\miniconda3\envs
pkgs_dirs:
  - F:\ProgramData\miniconda3\pkgs

conda config --show View configuration directory

Modify the default startup environment

notepad $PROFILE # Will open (if not automatically create) Profile.
conda activate notes # Add the following commands to the Profile file. Next time, open a terminal and the default Conda python environment will be notes .

This method works but it is a bit tasteless to enable immediate loading every time. Or honestly upgrade or downgrade the py version of base

View conda basic information

# View conda version information
conda info

# update conda version
conda update -n base conda

# Output conda configuration
conda config --show

Virtual environment management

Activate/Exit Environment

# activate
conda activate envname_xxx
conda activate env_path # Activate the environment of a specific path

# quit
conda deactivate

envname_xxx indicates the environment to be activated, if you forget the name, you can first use conda env list to view all virtual environments

Exit the environment If you do not continue to use this command line window, just close the window and leave it alone. You do not need to specify the environment name after closing the environment, and the current environment is closed by default.

Modify the creation environment location

The conda environment is installed in the user directory C:\Users\username.conda\envs by default. If you choose the default path, then create a virtual environment later and install it in the user directory. If you don’t want to take up space on the C drive, you can modify the conda virtual environment path.

The envs path under the anaconda installation directory was not found during creation, and the newly created environment is in C:\Users\xxx.conda\envs\

Pay attention to change the dividing line of the path to double slashes

Before adding the following two, you need to prescribe the corresponding directory permissions, otherwise it will cause problems that cannot be written

There is a .condarc file under C:\Users\username, open it, and add the following content at the end of it:

envs_dirs:
  -F://ProgramData//miniconda3//envs
pkgs_dirs:
  -F://ProgramData//miniconda3//pkgs

conda config --show View configuration directory

Existing environment view

conda-env list

New installations will only have one base default environment

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-cRdr4udU-1689435571658)(https://gitcode.net/qq_40597070/pic_go/raw/master/202301102332189.png )]

Create a new environment

conda create --name envname_xxx python=3.4
conda create -n envname_xxx python=3.4 # -n is the same shorthand for --name
conda create -n envname_xxx python=3.7 numpy=1.16. matplotlib # You can also install some other packages while creating the environment, or specify the version of the package at the same time
conda create -n envName python==3.7.3 pip --no-deps # create environment but do not install default packages

The above commands have the same effect, python34 refers to the name of the new environment to be created, python=3.4 refers to the python version of the creation environment, which will be automatically installed by default new

It should be noted that an error is often reported without permission to operate the directory, and the command line needs to be opened in the management mode

The third way to create pip is strongly recommended to install otherwise it will directly use the built-in pip

Delete environment

Delete an existing environment and all packages in it

conda remove --name envname_xxx --all

To delete an environment, another violent way is to directly delete the envs/env_name folder under the anaconda installation path

Copy specified environment

conda create -n new_env_name --clone old_clone_env_name

Copy the environment, copy old_env_name, and name the new environment new_clone_env_name

Copy the specified directory and replace old_env_name with the existing environment

The environment cannot be copied by copying the folder under envs in the anaconda installation directory, because such an operation will not change the configuration information in this environment by itself, and the operation of the copied environment will affect the original environment

View environment history

This function is similar to the version record of git, but this is only applicable to the package pip package installed by conda will not record

# View the specified environment history backtracking
conda list -n env_name -r

image-20230113002731625

The environment package falls back to the specified version

conda list -n env_name -r # View the specified environment history backtracking
conda install -n env_name --revision REV_NUMBER

First list all versions through the above command, and then return to a certain version through the following command

Environment renaming

# new version support method
conda-script.py rename [-h] [-n ENVIRONMENT | -p PATH] [--force] [-d] destination
conda rename -n current name new name

# example
conda rename -n test123 test321
conda rename --name test123 test321
conda rename -p path/to/test123 test321
conda rename --prefix path/to/test123 test321
conda create -n old_env_name --clone new_clone_env_name
conda remove -n old_env_name --all

There is currently no direct renaming command, you need to clone and then remove the old environment

Package management

List installed packages

conda list
# List all packages installed in the current environment
conda list --revisions
# List the version history of the package management changes in the current environment, list the version history of the current environment changes, this version record is similar to the git record, record the environment record after each package management operation, this command can save lives when it is critical, For example, if you upgrade a certain environment and find a problem and want to roll back to the previous environment state
conda list -n env_name
# List all packages in the specified environment
conda list --name env_name --revisions
# List the version history of package management changes for the specified environment

Installation package

conda install package_name
conda install PKGNAME==3.1.4 # Install the specified version of the package, one equal sign or two equal signs are acceptable
conda install --yes PKG1 PKG2 # Install the package and output the command prompt, that is, direct installation does not require confirmation, and it will be useful when writing automatically run scripts

Update package

# After starting Anaconda Prompt as administrator,
# upgrade is the same as update

conda update conda # upgrade conda (required before upgrading Anaconda)
conda update anaconda # upgrade anaconda
conda update --all # Update all packages, including Anaconda and conda
conda update --all -n env_name # Upgrade all packages in the specified environment, without -n env_name means upgrade all packages
conda update <package> # update the specified <package> package
conda update -h # other attempts
conda upgrade pip # upgrade pip

# pip package update
python -m pip install --upgrade pip
pip install --upgrade <package> # Upgrade the specified pip package, it is not recommended to use this method to upgrade the package installed by conda

Uninstall package

conda remove package_name
conda uninstall package_name # Uninstall a package in the current environment
conda uninstall -n env_name package_name # Uninstall a package in the specified environment

Clean unused caches and packages

conda clean --all

Sometimes you can easily clean up a few G, but if it is not a case of limited hard disk space, it is recommended not to clean up, because if the installation package has this package in the cache, there is no need to download it again

?

Environment package export and import

Export

Conda export needs to pay attention to the pip installed package will not be exported

# Export the activated environment package to YAML file
conda list --explicit > "C:\Users\Hi\Desktop\requirements.yaml"

# Export the activated environment package to a txt file
conda list --explicit > "C:\Users\Hi\Desktop\requirements.txt"
conda list -e > "C:\Users\Hi\Desktop\requirements.txt"
pip freeze > "C:\Users\Hi\Desktop\requirements.txt" # pip export

Import

# Create environment through yaml file
conda env create --file package_list.yaml # Create an environment from a YAML file
conda env create # There is a package_list.yml file in the current directory, you can directly create the environment without entering the file name

# Create an environment through a txt file
conda create --name new_env_name --file pkgs.txt

# Upgrade or install existing environment packages through yaml or txt
conda env update --file package_list.yaml # Install to the global environment (without activating the existing environment) the yaml or txt file exported by conda can be used
conda env update -n env_name --file package_list.yaml # Install to the specified environment, either yaml or txt files exported by conda
pip install -r requirements.txt # pip is installed to the current environment, and the txt file exported by pip is required

It should be noted that the txt imported by conda has more information than the txt imported by pip, and sometimes it is not universal

Use of Packages (package) and Channels (source/channel)

# Add Channels
conda config --add channels CHANNELNAME

# Use conda to search for packages from the configured Channels, you can bring the version number, the author's note: it is less used, and it is more convenient to directly search on Baidu/google under the system of the graphical interface
conda search numpy=1.16

# Use anaconda to search for packages from all Channels
anaconda search FUZZYNAME

# Install the package from the specified channel, such as
conda install conda-forge::PKGNAME