无需PIP即可离线安装相关的python模块

时间:2019-05-07 11:01:12

标签: python pip setuptools offline easy-install

编辑:这不是Python offline package installation的重复项,因为答案要求显示'pip'。我的前提是“点子”不可用。

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

我已经在本地计算机(具有Internet)上创建了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

我还使用install_requires创建了setup.py,其中包含了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",
    ],
)

我尝试运行以下命令来安装这些脚本(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 install --no-index --find-links /path/to/download/dir/ -r requirements.txt

但是,如何使它不使用点子

1 个答案:

答案 0 :(得分:0)

不使用pip进行安装需要您直接从tarball归档文件进行安装。所以,

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

详细信息:

  1. 使用pip freeze > requirements.txt
  2. 生成requirements.txt
  3. 从python环境中获取压缩包

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

        

      记住要下载目标计算机上缺少的所有内部依赖项。我还需要下载适用于Python 2.6.6的setuptools-0.6c9

  4. 将下载文件夹传输到生产机器(没有Internet和pip)

  5. 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脚本。