没有 PIP 的依赖 python 模块的离线安装

Offline installation of dependent python modules without PIP

编辑:这不是 Python offline package installation 的副本,因为答案需要 'pip' 存在。我的前提是 'pip' 不可用时

我的 python 脚本依赖于此 Github library。我需要创建一个包含此依赖项的自给自足的 tarball,我可以在我的生产服务器上提取和 运行,但 无法访问互联网或 pip。但是我有 Python 2.6.6/Python 2.7

我已经在我的本地机器(有互联网)上创建了一个 virtualenv 并使用 pip 安装在依赖之上。 pip 下载依赖库。我用

获得了 requirements.txt
pip freeze > requirements.txt

现在我使用

下载了这些要求
pip download -r requirements.txt

下载内容为

decorator-4.4.0-py2.py3-none-any.whl
jsonpath-rw-1.4.0.tar.gz  
jsonpath_rw_ext-1.2.0-py2.py3-none-any.whl  
pbr-5.2.0-py2.py3-none-any.whl 
ply-3.11-py2.py3-none-any.whl 
six-1.12.0-py2.py3-none-any.whl

我还创建了一个 setup.py,其中 install_requires 包含 requirements.txt 的所有内容(紧随其后 Python offline package installation

import setuptools

setuptools.setup(
    name="Resizing Automation Validation Script",
    packages=setuptools.find_packages(),
    install_requires=['ply','pbr','six','decorator','jsonpath-rw','jsonpath-rw-ext'],
    classifiers=[
        "Programming Language :: Python :: 2.6.6",
        "Operating System :: OS Independent",
    ],
)

我尝试 运行ning 以下命令来安装这些脚本(pip 不可用)

python setup.py develop --always-unzip --allow-hosts=None --find-links=/path/to/download/dir

注意:以上命令适用于本地新创建的 virtualenv。

但它在服务器上(没有互联网)失败并出现错误

running develop
running egg_info
creating Resizing_Automation_Validation_Script.egg-info
writing requirements to Resizing_Automation_Validation_Script.egg-info/requires.txt
writing Resizing_Automation_Validation_Script.egg-info/PKG-INFO
writing top-level names to Resizing_Automation_Validation_Script.egg-info/top_level.txt
writing dependency_links to Resizing_Automation_Validation_Script.egg-info/dependency_links.txt
writing manifest file 'Resizing_Automation_Validation_Script.egg-info/SOURCES.txt'
reading manifest file 'Resizing_Automation_Validation_Script.egg-info/SOURCES.txt'
writing manifest file 'Resizing_Automation_Validation_Script.egg-info/SOURCES.txt'
running build_ext
Creating /deployeruser/.local/lib/python2.7/site-packages/Resizing-Automation-Validation-Script.egg-link (link to .)
Adding Resizing-Automation-Validation-Script 1.0.0 to easy-install.pth file

Installed /deployeruser/tmp
Processing dependencies for Resizing-Automation-Validation-Script==1.0.0
Searching for jsonpath-rw-ext

Link to https://pypi.python.org/simple/jsonpath-rw-ext/ ***BLOCKED*** by --allow-hosts

Couldn't find index page for 'jsonpath-rw-ext' (maybe misspelled?)
Scanning index of all packages (this may take a while)

Link to https://pypi.python.org/simple/ ***BLOCKED*** by --allow-hosts

No local packages or download links found for jsonpath-rw-ext

使用 pip 效果很好

pip install --no-index --find-links /path/to/download/dir/ -r requirements.txt

但是,如何在没有 pip 的情况下使其工作

不使用 pip 安装需要您直接从 tarball 存档安装。所以,

  1. 首先,获取依赖项的所有 tarball 存档
  2. 将tar球转移到从属机器
  3. 将所有 tar 球提取到临时文件夹
  4. 使用 'python setup.py install --user'
  5. 安装
  6. 运行 程序:)

详情:

  1. 使用 pip freeze > requirements.txt
  2. 生成 requirements.txt
  3. 从您的python环境中获取tar球

    • cd 到您的下载文件夹
    • 运行pip download -r ../requirements.txt --no-binary :all:。这会将所有需求作为 tar.gz 存档下载到当前目录

      Remember to download all the inner dependencies that are missing from target machine. I needed to also download setuptools-0.6c9 for Python 2.6.6

  4. 将下载文件夹转移到生产机器(没有互联网和pip)

  5. cd 到下载文件夹和 运行 下面的命令将依赖项安装到当前活动的 python.

    install_tarball_python.sh [tar.gz-file]

#!/bin/bash

# Script: install_tarball_python
# takes the tar.gz dependency as arg
# creates a temp directory and extracts the archive in it.
# 'cd's into the extracted archive and runs 'python setup.py install'
# 'cd's back to the current directory and removes the temp containing the decompressed archive

if [ $# -lt 1 ]; then
    echo "Usage: install_tarball_python <package.tar.gz>"
    exit 1
fi
pushd . && mkdir temp && tar zxf  -C temp && cd temp && cd * && python setup.py install --user&& popd && rm -rf temp
  1. 运行 你的 python 脚本。