Python Package Managers

Sources:

  • miniconda

Python

Install Python3 on Debian/Ubuntu:

1
sudo apt install python3

////

Conda

推荐miniconda.

Install

->Install Conda

Install in China

If you're in China, you need to download conda from a mirror. Take the example from NJU Mirror:

1
wget https://mirror.nju.edu.cn/anaconda/miniconda/Miniconda3-py39_23.1.0-1-Linux-aarch64.sh

然后安装:

1
bash /path/to/miniconda

After installing, initialize your newly-installed Miniconda. The following commands initialize for bash and zsh shells:

1
2
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh

Switch source

If you're in China, then sometimes it's hard to download some libraries via conda. Instead, you can download libraries from a mirror site provided by tuna.

  1. Edit .condarc. In different platforms, it's at: - Linux: ${HOME}/.condarc - macOS: ${HOME}/.condarc - Windows: C:\Users\<YourUserName>\.condarc

  2. 编辑该文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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
    pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

    即可添加 Anaconda Python 免费仓库。

  3. 运行 conda clean -i 清除索引缓存,保证用的是镜像站提供的索引。

  4. 运行 conda create -n myenv numpy 测试一下吧

Commands

Display Conda environment information:

1
conda info

list all existing environments.

1
conda env list

指定channel:

1
conda install -c <channel> <library>

Create a new environment

1
conda create --name [project-env] python=3.7

The environments created by Anaconda is always located in ~/anaconda3/envs/( 对于Miniconda: ~/miniconda3/envs/)

  • 注意, 不要写成python==3.7. conda和pip不同. conda安装依赖只需要一个等号.

You may change the default location by using the following command but it is not encouraged. Conda can no longer find your environment by your environment name, you will have to specify the environment’s full path to activate it every time.

1
conda create --prefix /path/project-env

Activate your new environment:

1
conda activate project-env

display all packages in this environment:

1
conda list

conda list also supports revision history.:

1
conda list --revision

remove the environment:

1
conda env remove --name [project-env]

pip

pip是python的一个包管理工具,python2:使用pip, python3使用pip3

  • ( Python3下使用pip 默认用的是pip3 )

Install

1
yay -S pip3

mac用户:brew intall pip3相当卡, 因此要用:

1
curl bootstrap.pypa.io/get-pip.py | python3

检查安装是否成功:

1
pip3 --version

Switch source

Download from a mirror site temporally with -i <mirror site>:

1
pip3 install numpy -i https://mirrors.aliyun.com/pypi/simple/

Or, set global mirror site:

1
2
3
4
5
6
7
8
# 清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 阿里源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 腾讯源
pip config set global.index-url http://mirrors.cloud.tencent.com/pypi/simple

Commands

from a git repository

pip install a package from a git repository

1
pip install "Package" @ git+"URL of the repository"

e.g., If github repo https://github.com/AndrejOrsula/dreamerv3 has following arch:

1
2
3
4
5
6
7
8
9
10
11
12
13
.
├── dreamerv3
│   ├── <.....>
│   ├── __init__.py
├── example.py
├── LICENSE
├── MANIFEST.in
├── README.md
├── requirements.txt
├── scores
├── setup.py
└── test.py

Then this repo has a module dreamerv3. It can be downloaded by:

1
pip install --no-cache-dir "dreamerv3 @ git+https://github.com/AndrejOrsula/dreamerv3.git"

Don't use cache

1
pip install --no-cache-dir <package-name]>

It ensures that the package is always downloaded directly from the repository.

poetry

Installation

->Source

  1. Install poetry:

    1
    curl -sSL https://install.python-poetry.org | python3 -
  2. Verify installation:

    1
    poetry --version
  3. Enable tab completion for Bash, Fish, or Zsh. Suppose you're using oh-my-zsh and zsh, then

    1
    2
    mkdir $ZSH_CUSTOM/plugins/poetry
    poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetry

    and poetry to your plugins array in ~/.zshrc: