Anaconda
首先在 TUNA 下载 Anaconda 安装包。
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2020.11-Linux-x86_64.sh
bash Anaconda3-*.sh
根据提示完成安装,会发现 shell 的启动文件(~/.bashrc
、 ~/.zshrc
等等)中加入了 conda 的初始化脚本。
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('${HOME}/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "${HOME}/anaconda3/etc/profile.d/conda.sh" ]; then
. "${HOME}/anaconda3/etc/profile.d/conda.sh"
else
export PATH="${HOME}/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
为了加速 conda 下载速度,可以如下创建一个 ~/.condarc
,将 conda 源换成 TUNA
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
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
conda create -n ${ENV_NAME} python=3.8
conda activate ${ENV_NAME}
清华源 SSL 验证失败
报错信息为
$ conda create -n yolo python=3.8
Solving environment: failed
CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64/repodata.
json>
Elapsed: -
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
SSLError(MaxRetryError('HTTPSConnectionPool(host=\'mirrors.tuna.tsinghua.edu.cn\', port=443): Max retries exceeded with url: /ana$
onda/cloud/pytorch/linux-64/repodata.json (Caused by SSLError(SSLError("bad handshake: Error([(\'SSL routines\', \'ssl3_get_serve$
_certificate\', \'certificate verify failed\')])")))'))
参考 Anaconda建立新的环境,出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url … 解决过程,核心解决方案是将 https 协议切换为 http ,这样就能绕过 SSL 验证
channels:
- defaults
show_channel_urls: true
ssl_verify: true
default_channels:
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pip
参考 TUNA
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set global.find-links https://download.pytorch.org/whl/torch_stable.html
配置会写入 ~/.config/pip/pip.conf
。
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
find-links = https://download.pytorch.org/whl/torch_stable.html
将当前目录设置为安全
git config --global --add safe.directory ${project_root}
pip install -r requirements.txt
pyenv
参考 How to Manage your Python Projects with Pipenv & Pyenv 安装 pyenv
# MacOS
brew install pyenv pyenv-virtualenv
# Linux
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv > /dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
command -v pyenv > /dev/null || curl https://pyenv.run | bash
确保 pyenv 被初始化
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
使用 pyenv 安装 Python
pyenv versions --bare | grep -q ${python_version} || pyenv install ${python_version}
如果下载安装包过慢,可以通过 npmmirror 下载
wget https://registry.npmmirror.com/-/binary/python/3.11.2/Python-3.11.2.tar.xz -P ~/.pyenv/cache
如果提示缺少库文件,参考 wiki
sudo apt update
sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev \
libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
pipenv
参考 How to Manage your Python Projects with Pipenv & Pyenv 安装 pipenv
# MacOS
brew install pipenv
# all platforms
pip install pipenv
创建虚拟环境
cd ${project_root}
pipenv install --python ${python_version}
要将虚拟环境创建在项目内部,参考 How to set PIPENV_VENV_IN_PROJECT on per-project basis。
cd ${project_root}
mkdir .venv
touch .venv/.gitkeep
使用虚拟环境
# 进入 pipenv
pipenv shell
# 不进入 pipenv,直接执行命令
pipenv run ${cmd}
Ubuntu 安装多版本 Python
参考 ubuntu18.04通过apt安装python3.7.7与pip3.7最新版本(python3.8同理)
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.10
wget https://bootstrap.pypa.io/get-pip.py
python3.10 get-pip.py
sudo apt install python3.10-distutils
sudo apt install python3.10-dev
Ubuntu 22.04
sudo apt update
sudo apt install python3.10-venv
sudo apt install python3.10-pip