首页 > 解决方案 > 离线安装依赖python模块,无需PIP

问题描述

编辑:这不是Python 离线包安装的副本,因为答案需要存在“pip”。我的前提是当'pip'不可用时。

我的 python 脚本依赖于这个Github 库。我需要创建一个包含此依赖项的自给自足的 tarball,并且我可以在无法访问 internet 或 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 离线包安装开始)

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",
    ],
)

我尝试运行以下命令来安装这些脚本(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 的情况下使其工作

标签: pythonpipsetuptoolsofflineeasy-install

解决方案


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

  1. 首先,获取依赖项的所有 tarball 档案
  2. 将 tarball 传输到从属计算机
  3. 将所有压缩包解压到临时文件夹
  4. 使用“python setup.py install --user”安装
  5. 运行程序:)

细节:

  1. 使用生成 requirements.txtpip freeze > requirements.txt
  2. 从你的 python 环境中获取 tarball

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

      请记住下载目标机器中缺少的所有内部依赖项。我还需要为 Python 2.6.6下载setuptools-0.6c9

  3. 将下载文件夹转移到生产机器(无需互联网和pip)

  4. cd 下载文件夹并运行以下命令将依赖项安装到当前活动的 python。

    install_tarball_python.sh [tar.gz 文件]

#!/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 $1 -C temp && cd temp && cd * && python setup.py install --user&& popd && rm -rf temp
  1. 运行你的 python 脚本。

推荐阅读