Python之导出项目所需要的依赖库,在线或离线安装

1. 使用pip freeae

pip freeze > ./requirements.txt

该命令存在局限:

  • 该命令只会生成通过pip install 安装的包
  • 如果没有创建Python的虚拟环境virtualenv,该命令会将环境中所有的包都输出到requirements.txt文件,不管你当前的Project有没有用到这些包,适用于整个开发环境迁移

2. 使用pipreqs

Pipreqs 只会统计项目使用的包

2.1 安装pipreqs

pip install pipreqs

2.2 使用方法

在项目根目录中执行以下指令,生成requirements.txt

pipreqs "目录" --encoding=utf8 --force	# #在目录下生成requirements.txt --force覆盖原有的requirements.txt

比如:cd /data/python_project

执行:pipreqs /data/python_project

即可在项目根目录下看见requirements.txt文件,里面带有依赖库和版本号

2.3 特殊说明

2.3.1 Mac 下没法使用

pipreqs               
zsh: command not found: pipreqs

没有找到使用办法

2.3.2 Linux 使用报错

pipreqs ./
Traceback (most recent call last):
  File "/usr/bin/pipreqs", line 7, in 
    from pipreqs.pipreqs import main
  File "/usr/lib/python2.7/site-packages/pipreqs/pipreqs.py", line 51, in 
    from pipreqs import __version__
ImportError: cannot import name __version__

解决办法,更换pipreqs版本

# 查看可安装版本
pip install pipreqs==100
You are using pip version 7.1.0, however version 22.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting pipreqs==100
  Could not find a version that satisfies the requirement pipreqs==100 (from versions: 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2.5, 0.2.7, 0.2.8, 0.2.9, 0.3.0, 0.3.1, 0.3.2, 0.3.3, 0.3.4, 0.3.5, 0.3.6, 0.3.7, 0.3.8, 0.3.9, 0.4.0, 0.4.1, 0.4.2, 0.4.3, 0.4.4, 0.4.5, 0.4.6, 0.4.7, 0.4.8, 0.4.9, 0.4.10, 0.4.11)
No matching distribution found for pipreqs==100

# 选择某一版本安装
pip install pipreqs==0.4.0

# 查看能否使用
pipreqs --help
pipreqs - Generate pip requirements.txt file based on imports

Usage:
    pipreqs [options] 

Options:
    --use-local           Use ONLY local package info instead of querying PyPI
    --pypi-server         Use custom PyPi server
    --proxy               Use Proxy, parameter will be passed to requests library. You can also just set the
                          environments parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --encoding   Use encoding parameter for file open
    --savepath      Save the list of requirements in the given file
    --force               Overwrite existing requirements.txt


2.3.3 Windows下使用

pipreqs "目录" --encoding=utf8

3. 在线安装

复制 requirements.txt 到新机器,在所在目录执行命令安装,该方式为在线安装:

pip install -r requirements.txt

4. 离线安装

实际开发中,我们的项目部署环境可能是封闭的内网环境,无法直接使用pip install -r requirement.txt这种方式安装项目依赖包,这时pip download这个犀利的工具就要发光发热了。

4.1 下载安装包

下载安装包到require文件夹

4.1.1 单个下载

pip download Package -d Path	# Package:要下载的目标库,Path:下载保存路径

4.1.2 批量下载

pip download -d Path1 -r Path2\requirements.txt	# Path1:下载保存路径,Path2:requirements.txt文档所在路径

4.1.3 下载指定平台、指定Python版本的依赖包

实际开发中,我们可能使用的是Windows或者Mac系统开发的项目,但是需要将项目迁移到Linux下运行,这时我们需要导出项目所依赖的库,但是直接导出的库是匹配的我们当前使用的开发平台和Python版本的,直接复制到Linux平台是不能直接安装使用的。这时候需要我们再下载依赖包的时候指定目标平台和Python版本。

pip download \
    --only-binary=:all: \ # 对于包以及包的依赖包,都不使用源代码包
    --platform linux_x86_64 \  # 指定系统环境
    -d \home\packs   # 下载的文件的存储目录
    -r requirement.txt    # 指定要下载的包

    -i 表示使用指定的下载源(比如阿里源,当然可以使用其他国内源)
    --trusted-host 表示信任主机

有几个常用配置:

     –platform:指定需要安装的平台,比如:linux_x86_64,如果在windows/mac上默认会下载windows/mac的安装包,在linux上是肯定安装不了的
     –python-version:python的版本,默认与当前环境相同,如果值为3,则python版本为3.0.0, 若值为3.7,则python版本为3.7.0或3.7.3,最好根据python --version指定完整的版本号
注意:这里一定要添加--trusted-host,否则会报错:

WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.
ERROR: Could not find a version that satisfies the requirement xxx==xxx (from versions: none)
ERROR: No matching distribution found for xxx==xxx
#举例:通过清华源(可选项)安装pandas
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn pandas

#使用豆瓣源
pip install XXX 追加 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com 即可

下载指定的 linux 约束下的包及其依赖项

强制解释器是 py3k 的任何次要版本,并且只接受 abi 为 cp34m或 none 的 requests

$ pip download \

--only-binary=:all: \

--platform linux_x86_64 \

--python-version 3 \

--implementation cp \

--abi cp34m \

requests

$ ls requests*

requests-2.19.1-py2.py3-none-any.whl

备注:实际使用中,会发现一些包找不到,比如tornado,会报错如下

ERROR: Could not find a version that satisfies the requirement tornado==6.0 (from -r src/requirements.txt (line 13)) (from versions: none)
ERROR: No matching distribution found for tornado==6.0 (from -r src/requirements.txt (line 13))

原因:没有提供符合条件的二进制包

解决方法:使用非二进制包安装 --no-binary=:all: package_name

pip download --no-binary=:all: tornado==6.0 -d pkg/

pip download的其他详细使用方法请参考我的另一篇文章Python之pip download 命令用法-下载指定平台和python版本的依赖包_慕城南风的博客-CSDN博客

4.2 离线安装

复制require目录和requirements.txt 到新机器,在所在目录执行命令安装

单个安装

pip install Package

批量安装

pip install -r Path\requirement.txt

带依赖库安装

# 使用本地索引依赖包
pip install --no-index --find-links=Path Package	# Path:Package及依赖包存放路径;Package:待安装的带依赖包的库

或者

pip install --no-index --find-links=Path -r Path2\requirements.txt

5. pipdeptree查看依赖库

pipdeptree安装

pip install pipdeptree

列出依赖库

pipdeptree -p Package

列出哪些库依赖这些库

pipdeptree -p Package -r

你可能感兴趣的:(python,python)