Python开发环境配置

Python开发环境问题记录.

Python基础环境

Anaconda

conda config --add channels conda-forge
conda install -c conda-forge peewee

安装pip

python3 -m ensurepip

# 查看pip存储路径
pip3 -V

或者

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

# 检查版本
python -m pip --version

镜像加速

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

官方提供了一个python脚本,方便本地环境一键使用.

wget https://tuna.moe/oh-my-tuna/oh-my-tuna.py
python oh-my-tuna.py

在国内访问官方的库比较慢,建议使用国内的镜像站。
Windows配置在 %HOMEPATH%\pip\pip.ini

mkdir ~/.pip
vi ~/.pip/pip.conf
    [global]
    timeout = 6000
    index-url = https://pypi.doubanio.com/simple/
    trusted-host = pypi.doubanio.com
    
    [install]
    use-mirrors = true
    mirrors = https://pypi.doubanio.com/

清华镜像

# 临时使用
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pipenv
# 设为默认, 修改
# ~/pip/pip.conf (Linux)
# %APPDATA%\pip\pip.ini (Windows 10)
# $HOME/Library/Application Support/pip/pip.conf (macOS)
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

Anaconda镜像

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
# 第三方源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

Ubuntu

sudo apt install python-pip python-setuptools python-wheel

在Mac上同时安装Python2和Python3

缘起: 在High Sierra 10.12及更高版本,给系统自带的Python2安装一些第三方库(如gevent)会因为系统开启SIP导致失败.

SIP即系统完整性保护,要查看SIP的状态:> csrutil status

受到SIP保护的路径:/System, /usr, /bin, /sbin

比较好的方式是使用brew安装最新版的Python2/3

brew install python2
# 有问题重装
# brew reinstall python@2
brew install python3
# 安装之后也相应的安装了对应的pip2, pip3
python2 -V
pip2 -V
# 建立快捷方式
brew link python3

### pip3安装第三方库

# 需要添加到PATH
export PATH=/Users/xulz/Library/Python/3.6/bin:$PATH

开发环境

pip用法

pip/python package installer

  • –force-reinstall 重装所有包,即使已经最新
  • -U 更新/升级包
  • -I, –ignore-installed 重现安装包(忽略已安装)
  • -e, –editable <path/url> 从本地或VCS安装一个项目为编辑模式(setuptools开发模式)
# 安装git仓库
# @和#之间可以是tag或branch
pip install -e git://github.com/xulz/locust.git@0.13.6#egg=locustio
pip install some-package.whl
# 使用本地存储
pip install --use-wheel --no-index --find-links=/where/its/downloaded package_name


# 创建依赖
# 导出当前环境下的所有第三方库 
pip freeze > requirements.txt 
#将当前所有第三方库打成wheel包并保存在当前目录的backup文件夹下 
pip wheel --wheel-dir=".\backup" -r requirements.txt 
#从备份文件夹中安装第三方库 
pip install --use-wheel --no-index --find-links=".\backup" -r requirements.txt

本地开发包

# 在本地的site-packages创建指向开发环境的软链接
pip install -e .
#或者
python setup.py develop

requirements.txt

一些限定用法:

requests>=2.11.1
lxml>=3.8.0,!=4.2.0
ipaddress; python_version < '3.0'
colorama; sys_platform == 'win32'

环境依赖

Q: fatal error: Python.h: No such file or directory

A:

sudo apt install python3-dev

多环境使用

venv (Python3.3+)

系统自带的多环境管理工具,是virtualenv的精简版.

python3 -m venv env
source env/bin/activate
python3 -m pip install requests

deactivate

Poetry

python虚拟环境和包管理工具

注:默认安装方式网络受限

# 离线安装
git clone git@github.com:python-poetry/poetry.git
wget https://github.com/python-poetry/poetry/releases/download/1.1.13/poetry-1.1.13-linux.tar.gz
python3 get-poetry.py --file poetry-1.1.13-linux.tar.gz


# 安装路径 $HOME/.poetry/bin

当参数 virtualenvs.create=true 时,执行 poetry install 或 poetry add 时会检测当前项目是否有虚拟环境,没有就自动创建

# 新创建
poetry new

# 已有项目
poetry init

poetry run <command>

poetry env use python3

poetry shell

poetry show -h 

# 更新依赖
poetry update

poetry env list

poetry config 
poetry config --list
# poetry config后加--local来配置当前项目

pipenv

推荐用pipenv的方式方便使用.

# 安装
pip3 install pipenv

# 安装第三方库
pipenv install requests

# 在virtualenv运行
pipenv run python main.py
# 或激活virtualenv
pipenv shell

# 删除pipenv run创建的虚拟环境
pipenv --rm

# 安装依赖
# 对已有项目自动检查requirements.txt并转换为Pipfile
pipenv install

# 使用镜像
pipenv install --pypi-mirror https://pypi.tuna.tsinghua.edu.cn/simple
# 或者替换项目Pipfile对应url
# 也可以设置环境变量 PIPENV_PYPI_MIRROR

virtualenv

注: 不建议再使用virtualenv

安装:

pip3 install virtualenvwrapper
# Windows下使用
pip3 install virtualenvwrapper-win

使用

# 指定python版本
virtualenv -p python3 <project_name>


# 用法
source /usr/local/bin/virtualenvwrapper.sh
# WSL使用
source ~/.local/bin/virtualenvwrapper.sh


mkvirtualenv -h

workon

Troubleshooting

版本冲突

坑: 先发布了版本1.0, 之后发布了1.0.1, 结果pip install package_name总是得不到更新

原因: pip版本判断时会认为 1.0 > 1.0.1

最佳实践: 命名发布版本时要遵循x.x.x的格式

显示详细日志

pip install -vvv requests

locale.Error

Q: pip install get error: ’locale.Error: unsupported locale setting’
A: export LC_ALL=C, then check locale settings

参考