从pypi下载python包及其依赖项以离线安装到另一台机器的最佳方法是什么?使用pip或easy_install是否有简单的方法来做到这一点?我试图在一个没有连接到互联网的FreeBSD盒子上安装请求库。


当前回答

对于Windows,我使用了下面的东西

网络连接

1.创建任意名称的目录。用"repo"创建的

2.使用以下命令下载库(将下载而不是安装)

pip下载libraray_name -d"C:\repo"

pip download openpyxl -d"C:\repo"

然后您将发现多个.whl扩展名文件 复制requirements.txt中的所有文件名

没有互联网连接

现在将此文件夹和文件移动到没有互联网连接的PC并运行以下命令。

pip install -r requirements.txt --find-links=C:\repo --no-index

其他回答

对于Pip 8.1.2,您可以使用Pip download -r request .txt将包下载到您的本地机器。

如果您想离线安装python库及其依赖项,请在具有相同操作系统、网络连接并安装python的机器上完成以下步骤:

1)创建一个包含类似内容的requirements.txt文件(注意-这些是你希望下载的库):

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

创建需求文件的一个选项是使用pip freeze > requirements.txt。这将列出环境中的所有库。然后您可以进入requirements.txt,删除不需要的文件。

2)执行命令mkdir wheelhouse && pip download -r requirements.txt -d wheelhouse,将lib及其依赖项下载到目录wheelhouse

3)拷贝requirements.txt到wheelhouse目录

4)用tar -zcf wheelhouse.tar.gz归档wheelhouse到wheelhouse.tar.gz

然后上传wheelhouse.tar.gz到你的目标机器:

1)执行tar -zxf wheelhouse.tar.gz解压文件

2)执行pip install -r wheelhouse/requirements.txt——no-index——find-links wheelhouse安装lib及其依赖项

使用轮子编译包。

包:

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

复制tarball并安装:

$ 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/*

注意轮二进制包不是跨机器的。

更多参考资料请访问:https://pip.pypa.io/en/stable/user_guide/#installation-bundles

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

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

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

假设您在~/src/myapp中有一个格式正确的Python应用程序。~/src/myapp/setup.py将有install_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目录中没有一个或多个依赖项,构建将失败。它不会试图从互联网上乱安装丢失的东西。

我强烈建议调用setup.py develop…和easy_install…在活动虚拟环境中,以避免污染您的全局Python环境。这是(virtualenv)应该走的路。不要在全局Python环境中安装任何东西。

If the machine that you've built your app has same architecture as the machine on which you want to deploy it, you can simply tarball the entire virtual environment directory into which you easy_install-ed everything. Just before tarballing though, you must make the virtual environment directory relocatable (see --relocatable option). NOTE: the destination machine needs to have the same version of Python installed, and also any C-based dependencies your app may have must be preinstalled there too (e.g. say if you depend on PIL, then libpng, libjpeg, etc must be preinstalled).

下载tarball,将它转移到你的FreeBSD机器并提取它,然后运行python setup.py install,你就完成了!

编辑:在此基础上,您现在还可以使用pip安装焦油球。