Skip to content

Latest commit

 

History

History
230 lines (164 loc) · 4.35 KB

python.md

File metadata and controls

230 lines (164 loc) · 4.35 KB

python、pip相关

1.pyenv管理python版本和虚拟环境

  1. 克隆项目
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
  1. 配置环境变量
# bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
# zsh
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc
  1. 重新初始化shell环境
exec $SHELL
  1. 安装 pyenv-virtualenv
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
  1. 配置环境变量
# bash
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
# zsh
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc
  1. 重新初始化shell环境
exec $SHELL
  1. 安装python 3.7.7
pyenv install 3.7.7
  1. 如果安装失败,安装相应的依赖后重试
# Ubuntu/Debian/Mint
sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
# CentOS/Fedora 21 and below
yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel
# Fedora 22 and above
dnf install make gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel

更多请参考pyenv官方文档

  1. 创建、激活、删除虚拟环境
pyenv virtualenv 3.7.7 venv
pyenv activate venv
# 取消激活
pyenv deactivate
# 删除
pyenv uninstall venv
  1. 其他命令
# 查看所有已安装的python环境
pyenv versions
# 查看所有可安装的python环境
pyenv install [Tab键]
# 为test目录设置默认python环境
cd ~/test && pyenv local venv
# 设置全局环境
pyenv global 3.7.7 # 设置3.7.7为全局python环境
# 为当前shell指定python版本
pyenv shell 3.7.7
# 取消当前shell的python版本
unset PYENV_VERSION

2.升级pip后出现ImportError: cannot import name main

解决办法:
用vim打开pip文件

sudo vim /usr/bin/pip

把如下内容:

from pip import main
if __name__ == '__main__':
    sys.exit(main())

修改为:

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

3.豆瓣源安装pip插件

sudo pip install 插件名 -i https://pypi.douban.com/simple

4.生成和安装requirements.txt依赖

  • 生成requirements.txt
pip freeze > requirements.txt
  • 安装requirements.txt依赖
pip install -r requirements.txt
# 使用豆瓣源
pip install -r requirements.txt -i https://pypi.douban.com/simple

5.python3虚拟环境操作

  1. 安装python3-venv
sudo apt-get install python3-venv
  1. 创建虚拟环境
python3 -m venv 虚拟环境名
  1. 激活虚拟环境
source 虚拟环境名/bin/activate
  1. 退出虚拟环境
deactivate

6.python2虚拟环境操作

  1. 安装virtualenv
sudo apt install virtualenv
  1. 创建虚拟环境
virtualenv 虚拟环境名
  1. 激活虚拟环境
source 虚拟环境名/bin/activate
  1. 退出虚拟环境
deactivate

7.安装python3.7报错No module named '_ctypes'

1、安装libffi-dev

sudo apt install libffi-dev

2、安装其他所需组件

sudo apt-get install libbz2-dev libreadline-dev libsqlite3-dev

8.更换pip为豆瓣源

1、在主目录下创建.pip文件夹,然后在该目录下创建pip.conf文件

mkdir ~/.pip
vim ~/.pip/pip.conf

2、pip.conf文件编写如下内容,保存退出即可

[global]
index-url = https://pypi.douban.com/simple 

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