2022年 11月 3日

Python 项目包管理

一、项目包管理:

  • 生成所需要用到的扩展包,到当前目录。
pip freeze > requirements.txt
  • requirements.txt(可以用 # 注释非必须包)
  1. xxx==版本号
  2. numpy==1.12.1
  3. # jupyter==1.0.0
  4. # matplotlib==1.5.3
  • 指定txt目录,安装依赖
pip install -r requirements.txt

二、指定阿里云

1. 指定阿里云镜像, 安装requirements.txt中的所有

pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com -r requirements.txt

2.指定阿里云镜像,安装flask / pandas

pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com flask
python -m pip install -U -i http://mirrors.aliyun.com/pypi/simple pandas

三、其他镜像

国内源:

新版ubuntu要求使用https源,要注意。

清华:Simple Index

阿里云:Simple Index

中国科技大学 Simple Index

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/ 

豆瓣:Simple Index

临时使用:

可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple

例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider,这样就会从清华这边的镜像去安装pyspider库。
查看当前所用镜像源

pip config list

永久修改,一劳永逸:

Linux下,修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)

内容如下:

  1. [global]
  2. index-url = https://pypi.tuna.tsinghua.edu.cn/simple
  3. ## mirrors.aliyun.com
  4. [install]
  5. trusted-host = tsinghua.edu.cn
  6. # 代理 内网环境若需IP端口代理自行更改,或者开通全局代理
  7. # proxy = http://192.168.20.12:3308
  8. # 时效性
  9. timeout = 60

windows下,直接在user目录中创建一个pip目录,如:C:\Users\xx\AppData\Roaming\pip,新建文件pip.ini。内容同上。

1、win+R

2、输入 %APPDATA%,确定xx路径啦,譬如

3、创建pip文件夹,打开文件夹创建pip.ini文件

4、编辑pip.ini,输入同上:

或者直接修改:

  1. pip install pip -U
  2. pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple