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 | ~/miniconda3/bin/conda init bash |
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.
Edit
.condarc. In different platforms, it's at: - Linux:${HOME}/.condarc- macOS:${HOME}/.condarc- Windows:C:\Users\<YourUserName>\.condarc编辑该文件:
1
2
3
4
5
6
7
8
9
10channels:
- 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 免费仓库。
运行
conda clean -i清除索引缓存,保证用的是镜像站提供的索引。运行
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 | # 清华源 |
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 | . |
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
Install poetry:
1
curl -sSL https://install.python-poetry.org | python3 -
Verify installation:
1
poetry --version
Enable tab completion for Bash, Fish, or Zsh. Suppose you're using oh-my-zsh and zsh, then
1
2mkdir $ZSH_CUSTOM/plugins/poetry
poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetryand
poetryto your plugins array in~/.zshrc: