237

从 pypi 下载 python 包及其依赖项以在另一台机器上离线安装的最佳方法是什么?使用 pip 或 easy_install 有什么简单的方法吗?我正在尝试在未连接到 Internet 的 FreeBSD 机器上安装请求库。

4

12 回答 12

395

在可以访问互联网的系统上

pipdownload命令允许您下载包而不安装它们:

pip download -r requirements.txt

(在以前的 pip 版本中,拼写为pip install --download -r requirements.txt。)

在无法访问互联网的系统上

然后你可以使用

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

安装那些下载的模块,无需访问网络。

于 2013-01-21T20:55:53.190 回答
135

If you want install python libs and their dependencies offline, finish following these steps on a machine with the same os, network connected, and python installed:

1) Create a requirements.txt file with similar content (Note - these are the libraries you wish to download):

Flask==0.12
requests>=2.7.0
scikit-learn==0.19.1
numpy==1.14.3
pandas==0.22.0

One option for creating the requirements file is to use pip freeze > requirements.txt. This will list all libraries in your environment. Then you can go in to requirements.txt and remove un-needed ones.

2) Execute command mkdir wheelhouse && pip download -r requirements.txt -d wheelhouse to download libs and their dependencies to directory wheelhouse

3) Copy requirements.txt into wheelhouse directory

4) Archive wheelhouse into wheelhouse.tar.gz with tar -zcf wheelhouse.tar.gz wheelhouse

Then upload wheelhouse.tar.gz to your target machine:

1) Execute tar -zxf wheelhouse.tar.gz to extract the files

2) Execute pip install -r wheelhouse/requirements.txt --no-index --find-links wheelhouse to install the libs and their dependencies

于 2018-08-02T05:50:12.977 回答
84

如果包在 PYPI 上,请将其及其依赖项下载到某个本地目录。例如

$ mkdir /pypi && cd /pypi
$ ls -la
  -rw-r--r-- 1 个铺路人员 237954 Apr 19 11:31 Flask-WTF-0.6.tar.gz
  -rw-r--r-- 1 名铺路人员 389741 Feb 22 17:10 Jinja2-2.6.tar.gz
  -rw-r--r-- 1 名铺路人员 70305 Apr 11 00:28 MySQL-python-1.2.3.tar.gz
  -rw-r--r-- 1 个铺路人员 2597214 Apr 10 18:26 SQLAlchemy-0.7.6.tar.gz
  -rw-r--r-- 1 名铺路人员 1108056 2 月 22 日 17:10 Werkzeug-0.8.2.tar.gz
  -rw-r--r-- 1 名铺路人员 488207 Apr 10 18:26 boto-2.3.0.tar.gz
  -rw-r--r-- 1 名铺路人员 490192 Apr 16 12:00 flask-0.9-dev-2a6c80a.tar.gz

某些包可能必须手动归档到外观相似的 tarball 中。当我想要更新(不太稳定)版本的东西时,我经常这样做。有些包不在 PYPI 上,所以同样适用于它们。

假设您有一个格式正确的 Python 应用程序~/src/myapp~/src/myapp/setup.pyinstall_requires列出提及您/pypi目录中的一项或多项内容的列表。像这样:

  install_requires=[
    'boto',
    'Flask',
    'Werkzeug',
    # and so on

如果您希望能够运行具有所有必要依赖项的应用程序,同时仍然对其进行破解,您将执行以下操作:

$ cd ~/src/myapp
$ python setup.py develop --always-unzip --allow-hosts=None --find-links=/pypi

这样,您的应用程序将直接从您的源目录执行。你可以破解一些东西,然后重新运行应用程序而不重建任何东西。

如果您想将您的应用程序及其依赖项安装到当前的 python 环境中,您将执行以下操作:

$ cd ~/src/myapp
$ easy_install --always-unzip --allow-hosts=None --find-links=/pypi 。

在这两种情况下,如果目录中不存在一个或多个依赖项,则构建将失败/pypi。它不会试图乱七八糟地安装 Internet 上丢失的东西。

我强烈建议在活动的虚拟环境中调用setup.py develop ...并避免污染您的全局 Python 环境。这是(virtualenv 就是)几乎要走的路。永远不要在全局 Python 环境中安装任何东西。easy_install ...

如果您构建应用程序的机器与您要在其上部署应用程序的机器具有相同的架构,您可以简单地将整个虚拟环境目录压缩到其中,将easy_install所有内容放入其中。不过,在打包之前,您必须使虚拟环境目录可重定位(请参阅--relocatable选项)。注意:目标机器需要安装相同版本的 Python,并且您的应用程序可能具有的任何基于 C 的依赖项也必须预先安装在那里(例如,如果您依赖PIL,则必须预先安装 libpng、libjpeg 等) .

于 2012-06-18T22:32:55.500 回答
17
于 2017-08-10T18:19:16.027 回答
13

Let me go through the process step by step:

  1. On a computer connected to the internet, create a folder.
   $ mkdir packages
   $ cd packages
  1. open up a command prompt or shell and execute the following command:

    Suppose the package you want is tensorflow

    $ pip download tensorflow

  2. Now, on the target computer, copy the packages folder and apply the following command

  $ cd packages
  $ pip install 'tensorflow-xyz.whl' --no-index --find-links '.'

Note that the tensorflow-xyz.whl must be replaced by the original name of the required package.

于 2019-10-21T14:53:17.060 回答
10

I had a similar problem. And i had to make it install the same way, we do from pypi.

I did the following things:

  1. Make a directory to store all the packages in the machine that have internet access.

    mkdir -p /path/to/packages/
    
  2. Download all the packages to the path

Edit: You can also try:

python3 -m pip wheel --no-cache-dir -r requirements.txt -w /path/to/packages
pip download -r requirements.txt -d /path/to/packages

Eg:- ls /root/wheelhouse/  # **/root/wheelhouse** is my **/path/to/packages/**
total 4524
-rw-r--r--. 1 root root   16667 May 23  2017 incremental-17.5.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root   34713 Sep  1 10:21 attrs-18.2.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 3088398 Oct 15 14:41 Twisted-18.9.0.tar.bz2
-rw-r--r--. 1 root root  133356 Jan 28 15:58 chardet-3.0.4-py2.py3-none-any.whl
-rw-r--r--. 1 root root  154154 Jan 28 15:58 certifi-2018.11.29-py2.py3-none-any.whl
-rw-r--r--. 1 root root   57987 Jan 28 15:58 requests-2.21.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root   58594 Jan 28 15:58 idna-2.8-py2.py3-none-any.whl
-rw-r--r--. 1 root root  118086 Jan 28 15:59 urllib3-1.24.1-py2.py3-none-any.whl
-rw-r--r--. 1 root root   47229 Jan 28 15:59 tqdm-4.30.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root    7922 Jan 28 16:13 constantly-15.1.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root  164706 Jan 28 16:14 zope.interface-4.6.0-cp27-cp27mu-manylinux1_x86_64.whl
-rw-r--r--. 1 root root  573841 Jan 28 16:14 setuptools-40.7.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root   37638 Jan 28 16:15 Automat-0.7.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root   37905 Jan 28 16:15 hyperlink-18.0.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root   52311 Jan 28 16:15 PyHamcrest-1.9.0-py2.py3-none-any.whl
 -rw-r--r--. 1 root root   10586 Jan 28 16:15 six-1.12.0-py2.py3-none-any.whl
  1. Tar the packages directory and copy it to the Machine that doesn't have internet access. Then do,

    cd /path/to/packages/
    tar -cvzf packages.tar.gz .  # not the . (dot) at the end
    

Copy the packages.tar.gz into the destination machine that doesn't have internet access.

  1. In the machine that doesn't have internet access, do the following (Assuming you copied the tarred packages to /path/to/package/ in the current machine)

    cd /path/to/packages/
    tar -xvzf packages.tar.gz
    mkdir -p $HOME/.config/pip/
    vi $HOME/.config/pip/pip.conf
    

and paste the following content inside and save it.

[global]
timeout = 10
find-links = file:///path/to/package/
no-cache-dir = true
no-index = true
  1. Finally, i suggest you use, some form of virtualenv for installing the packages.

    virtualenv -p python2 venv # use python3, if you are on python3
    source ./venv/bin/activate
    pip install <package>
    

You should be able to download all the modules that are in the directory /path/to/package/.

Note: I only did this, because i couldn't add options or change the way we install the modules. Otherwise i'd have done

pip install --no-index --find-links /path/to/download/dir/ -r requirements.txt
于 2019-01-28T11:56:39.317 回答
7

下载tarball,将其传输到您的 FreeBSD 机器并解压,然后运行python setup.py install,您就完成了!

编辑:再补充一点,您现在也可以使用 pip 安装 tarball。

于 2012-06-18T21:54:36.610 回答
7

Using wheel compiled packages.

bundle up:

$ tempdir=$(mktemp -d /tmp/wheelhouse-XXXXX)
$ pip wheel -r requirements.txt --wheel-dir=$tempdir
$ cwd=`pwd`
$ (cd "$tempdir"; tar -cjvf "$cwd/bundled.tar.bz2" *)

copy tarball and install:

$ tempdir=$(mktemp -d /tmp/wheelhouse-XXXXX)
$ (cd $tempdir; tar -xvf /path/to/bundled.tar.bz2)
$ pip install --force-reinstall --ignore-installed --upgrade --no-index --no-deps $tempdir/*

Note wheel binary packages are not across machines.

More ref. here: https://pip.pypa.io/en/stable/user_guide/#installation-bundles

于 2017-09-20T02:46:37.830 回答
3

对于 Pip 8.1.2,您可以使用pip download -r requ.txt将包下载到本地计算机。

于 2016-11-15T07:08:15.793 回答
3

As a continues to @chaokunyang answer, I want to put here the script I write that does the work:

  1. Write a "requirements.txt" file that specifies the libraries you want to pack.
  2. Create a tar file that contains all your libraries (see the Packer script).
  3. Put the created tar file in the target machine and untar it.
  4. run the Installer script (which is also packed into the tar file).

"requirements.txt" file

docker==4.4.0

Packer side: file name: "create-offline-python3.6-dependencies-repository.sh"

#!/usr/bin/env bash

# This script follows the steps described in this link:
# https://stackoverflow.com/a/51646354/8808983

LIBRARIES_DIR="python3.6-wheelhouse"

if [ -d ${LIBRARIES_DIR} ]; then
    rm -rf ${LIBRARIES_DIR}/*
else
    mkdir ${LIBRARIES_DIR}
fi

pip download -r requirements.txt -d ${LIBRARIES_DIR}

files_to_add=("requirements.txt" "install-python-libraries-offline.sh")

for file in "${files_to_add[@]}"; do
    echo "Adding file ${file}"
    cp "$file" ${LIBRARIES_DIR}
done

tar -cf ${LIBRARIES_DIR}.tar ${LIBRARIES_DIR}

Installer side: file name: "install-python-libraries-offline.sh"

#!/usr/bin/env bash

# This script follows the steps described in this link:
# https://stackoverflow.com/a/51646354/8808983

# This file should run during the installation process from inside the libraries directory, after it was untared:
# pythonX-wheelhouse.tar -> untar -> pythonX-wheelhouse
# |
# |--requirements.txt
# |--install-python-libraries-offline.sh


pip3 install -r requirements.txt --no-index --find-links .
于 2020-11-24T16:36:34.927 回答
1

Download the wheel file (for example dlb-0.5.0-py3-none-any.whl) from Pypi and

pip install dlb-0.5.0-py3-none-any.whl
于 2020-08-21T08:24:47.533 回答
0

For Windows I have used below things

Internet Connection

1.Create directory with any name.I have created with "repo"

2.Download libraries using below command (it will download not install)

pip download libraray_name -d"C:\repo"

pip download openpyxl -d"C:\repo"
  1. Then you will find mulitple .whl extention files

  2. copy all the filename in requirements.txt enter image description here

No Internet Connection

  1. Now Move this folder and files to PC where no internet connection and run below command.
pip install -r requirements.txt --find-links=C:\repo --no-index
于 2021-12-16T03:39:54.370 回答