简体   繁体   English

如何离线安装包?

[英]How to install packages offline?

What's the best way to download a python package and it's dependencies from pypi for offline installation on another machine?从 pypi 下载 python 包及其依赖项以在另一台机器上离线安装的最佳方法是什么? Is there any easy way to do this with pip or easy_install?使用 pip 或 easy_install 有什么简单的方法吗? I'm trying to install the requests library on a FreeBSD box that is not connected to the internet.我正在尝试在未连接到 Internet 的 FreeBSD 机器上安装请求库。

On the system that has access to internet在可以访问互联网的系统上

The pip download command lets you download packages without installing them: pip download命令让您无需安装即可下载软件包:

pip download -r requirements.txt

(In previous versions of pip, this was spelled pip install --download -r requirements.txt .) (在以前的 pip 版本中,拼写为pip install --download -r requirements.txt 。)

On the system that has no access to internet在无法访问互联网的系统上

Then you can use然后你可以使用

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

to install those downloaded modules, without accessing the network.安装那些下载的模块,无需访问网络。

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:如果要离线安装 python 库及其依赖项,请在安装了相同操作系统、网络连接和 python 的机器上完成以下步骤:

1) Create a requirements.txt file with similar content (Note - these are the libraries you wish to download): 1) 创建一个包含类似内容的requirements.txt文件(注意 - 这些是您要下载的库):

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 .创建需求文件的一种选择是使用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.然后你可以进入requirements.txt并删除不需要的。

2) Execute command mkdir wheelhouse && pip download -r requirements.txt -d wheelhouse to download libs and their dependencies to directory wheelhouse 2) 执行命令mkdir wheelhouse && pip download -r requirements.txt -d wheelhouse将库及其依赖项下载到目录wheelhouse

3) Copy requirements.txt into wheelhouse directory 3) 将 requirements.txt 复制到wheelhouse目录中

4) Archive wheelhouse into wheelhouse.tar.gz with tar -zcf wheelhouse.tar.gz wheelhouse 4) 使用tar -zcf wheelhouse.tar.gz wheelhouse存档到wheelhouse.tar.gz

Then upload wheelhouse.tar.gz to your target machine:然后将wheelhouse.tar.gz上传到您的目标机器:

1) Execute tar -zxf wheelhouse.tar.gz to extract the files 1) 执行tar -zxf wheelhouse.tar.gz解压文件

2) Execute pip install -r wheelhouse/requirements.txt --no-index --find-links wheelhouse to install the libs and their dependencies 2) 执行pip install -r wheelhouse/requirements.txt --no-index --find-links wheelhouse安装库及其依赖项

If the package is on PYPI, download it and its dependencies to some local directory.如果包在 PYPI 上,请将其及其依赖项下载到某个本地目录。 Eg例如

$ 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

Some packages may have to be archived into similar looking tarballs by hand.某些包可能必须手动归档到外观相似的 tarball 中。 I do it a lot when I want a more recent (less stable) version of something.当我想要更新(不太稳定)版本的东西时,我经常这样做。 Some packages aren't on PYPI, so same applies to them.有些包不在 PYPI 上,所以同样适用于它们。

Suppose you have a properly formed Python application in ~/src/myapp .假设您在~/src/myapp中有一个格式正确的 Python 应用程序。 ~/src/myapp/setup.py will have install_requires list that mentions one or more things that you have in your /pypi directory. ~/src/myapp/setup.py将具有install_requires列表,其中提到您在/pypi目录中拥有的一项或多项内容。 Like so:像这样:

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

If you want to be able to run your app with all the necessary dependencies while still hacking on it, you'll do something like this:如果您希望能够运行具有所有必要依赖项的应用程序,同时仍然对其进行破解,您将执行以下操作:

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

This way your app will be executed straight from your source directory.这样,您的应用程序将直接从您的源目录执行。 You can hack on things, and then rerun the app without rebuilding anything.你可以破解一些东西,然后重新运行应用程序而不重建任何东西。

If you want to install your app and its dependencies into the current python environment, you'll do something like this:如果您想将您的应用程序及其依赖项安装到当前的 python 环境中,您将执行以下操作:

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

In both cases, the build will fail if one or more dependencies aren't present in /pypi directory.在这两种情况下,如果/pypi目录中不存在一个或多个依赖项,则构建将失败。 It won't attempt to promiscuously install missing things from Internet.它不会试图乱七八糟地安装 Internet 上丢失的东西。

I highly recommend to invoke setup.py develop ... and easy_install ... within an active virtual environment to avoid contaminating your global Python environment.我强烈建议在活动的虚拟环境中调用setup.py develop ...easy_install ...以避免污染您的全局 Python 环境。 It is (virtualenv that is) pretty much the way to go.这是(virtualenv 就是)几乎要走的路。 Never install anything into global Python environment.永远不要在全局 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.如果您构建应用程序的机器与您要在其上部署它的机器具有相同的体系结构,您可以简单地将您easy_install的所有内容的整个虚拟环境目录压缩到其中。 Just before tarballing though, you must make the virtual environment directory relocatable (see --relocatable option).不过,在打包之前,您必须使虚拟环境目录可重定位(请参阅--relocatable选项)。 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 (eg say if you depend on PIL , then libpng, libjpeg, etc must be preinstalled).注意:目标机器需要安装相同版本的 Python,并且您的应用程序可能具有的任何基于 C 的依赖项也必须预先安装在那里(例如,如果您依赖PIL ,则必须预先安装 libpng、libjpeg 等) .

Let me go through the process step by step:让我一步一步地完成这个过程:

  1. On a computer connected to the internet, create a folder.在连接到 Internet 的计算机上,创建一个文件夹。
   $ mkdir packages
   $ cd packages
  1. open up a command prompt or shell and execute the following command:打开命令提示符或 shell 并执行以下命令:

    Suppose the package you want is tensorflow假设你想要的包是tensorflow

    $ pip download tensorflow

  2. Now, on the target computer, copy the packages folder and apply the following command现在,在目标计算机上,复制packages文件夹并应用以下命令

  $ 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.请注意, tensorflow-xyz.whl必须替换为所需包的原始名称。

offline python.离线蟒蛇。 for doing this I use virtualenv (isolated Python environment)为此,我使用 virtualenv(隔离 Python 环境)

1) install virtualenv online with pip: 1)使用pip在线安装virtualenv:

pip install virtualenv --user

or offline with whl: go to this link , download last version (.whl or tar.gz) and install that with this command:或使用 whl 离线:转到此链接,下载最新版本(.whl 或 tar.gz)并使用以下命令安装:

pip install virtualenv-15.1.0-py2.py3-none-any.whl --user

by using --user you don't need to use sudo pip… .通过使用--user你不需要使用sudo pip…

2) use virtualenv 2)使用虚拟环境

on online machine select a directory with terminal cd and run this code:在在线机器上选择带有终端cd的目录并运行以下代码:

python -m virtualenv myenv
cd myenv
source bin/activate
pip install Flask

after installing all the packages, you have to generate a requirements.txt so while your virtualenv is active, write安装完所有包后,你必须生成一个requirements.txt所以当你的 virtualenv 处于活动状态时,写

pip freeze > requirements.txt

open a new terminal and create another env like myenv2 .打开一个新终端并创建另一个环境,例如myenv2

python -m virtualenv myenv2
cd myenv2
source bin/activate
cd -
ls

now you can go to your offline folder where your requirements.txt and tranferred_packages folder are in there.现在您可以转到您的requirements.txttranferred_packages文件夹所在的离线文件夹。 download the packages with following code and put all of them to tranferred_packages folder.使用以下代码下载软件包并将它们全部放入tranferred_packages文件夹。

pip download -r requirements.txt

take your offline folder to offline computer and then将您的脱机文件夹带到脱机计算机,然后

python -m virtualenv myenv2
cd myenv2
source bin/activate
cd -
cd offline
pip install --no-index --find-links="./tranferred_packages" -r requirements.txt

what is in the folder offline [requirements.txt , tranferred_packages {Flask-0.10.1.tar.gz, ...}]离线文件夹中有什么 [requirements.txt, tranferred_pa​​ckages {Flask-0.10.1.tar.gz, ...}]

check list of your package检查您的包裹清单

pip list

note: as we are in 2017 it is better to use python 3. you can create python 3 virtualenv with this command.注意:因为我们在 2017 年,最好使用 python 3。您可以使用此命令创建 python 3 virtualenv。

virtualenv -p python3 envname

I had a similar problem.我有一个类似的问题。 And i had to make it install the same way, we do from pypi.我必须让它以同样的方式安装,我们从 pypi 做。

I did the following things:我做了以下事情:

  1. Make a directory to store all the packages in the machine that have internet access.创建一个目录来存储可以访问 Internet 的机器中的所有包。

     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.打包包目录并将其复制到无法访问 Internet 的机器上。 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.packages.tar.gz复制到无法访问 Internet 的目标计算机中。

  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)在无法访问 Internet 的机器上,执行以下操作(假设您将 tarred 包复制到当前机器中的/path/to/package/中)

     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来安装软件包。

     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/ .您应该能够下载目录/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

Download the tarball , transfer it to your FreeBSD machine and extract it, afterwards run python setup.py install and you're done!下载tarball ,将其传输到您的 FreeBSD 机器并解压,然后运行python setup.py install就完成了!

EDIT: Just to add on that, you can also install the tarballs with pip now.编辑:再补充一点,您现在也可以使用 pip 安装 tarball。

Using wheel compiled packages.使用wheel编译包。

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.注意wheel二进制包不跨机器。

More ref.更多参考。 here: https://pip.pypa.io/en/stable/user_guide/#installation-bundles在这里: https ://pip.pypa.io/en/stable/user_guide/#installation-bundles

As a continues to @chaokunyang answer, I want to put here the script I write that does the work:作为对@chaokunyang 的继续回答,我想把我写的可以完成工作的脚本放在这里:

  1. Write a "requirements.txt" file that specifies the libraries you want to pack.编写一个“requirements.txt”文件,指定要打包的库。
  2. Create a tar file that contains all your libraries (see the Packer script).创建一个包含所有库的 tar 文件(请参阅 Packer 脚本)。
  3. Put the created tar file in the target machine and untar it.将创建的 tar 文件放入目标机器并解压缩。
  4. run the Installer script (which is also packed into the tar file).运行安装程序脚本(它也被打包到 tar 文件中)。

"requirements.txt" file “requirements.txt”文件

docker==4.4.0

Packer side: file name: "create-offline-python3.6-dependencies-repository.sh" Packer 端:文件名:“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"安装程序端:文件名:“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 .

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

从 Pypi 下载轮子文件(例如 dlb-0.5.0-py3-none-any.whl)并

pip install dlb-0.5.0-py3-none-any.whl

For downloading a package with all his dependencies, create a folder Mypackage,要下载包含所有依赖项的包,请创建一个文件夹 Mypackage,
Move to this folder:移至此文件夹:

cd Mypackage

After download your package with pip:使用 pip 下载软件包后:

pip download PackageName

In the offline machine, load the folder and run:在离线机器上,加载文件夹并运行:

for %x in (dir *.whl) do python -m pip install %x

you can check this How to install multiple whl files in cmd您可以查看如何在 cmd 中安装多个 whl 文件

Make sure you use the same version of python, or add version command to "pip download"确保使用相同版本的python,或在“pip下载”中添加版本命令

For Windows I have used below things对于 Windows,我使用了以下内容

Internet Connection网络连接

1.Create directory with any name.I have created with "repo" 1.创建任何名称的目录。我用“repo”创建

2.Download libraries using below command (it will download not install) 2.使用以下命令下载库(它将下载而不是安装)

pip download libraray_name -d"C:\repo" pip 下载库名称 -d"C:\repo"

pip download openpyxl -d"C:\repo"
  1. Then you will find mulitple .whl extention files然后你会发现多个 .whl 扩展文件

  2. copy all the filename in requirements.txt复制 requirements.txt 中的所有文件名在此处输入图像描述

No Internet Connection没有网络连接

  1. Now Move this folder and files to PC where no internet connection and run below command.现在将此文件夹和文件移动到没有互联网连接的 PC 并运行以下命令。
pip install -r requirements.txt --find-links=C:\repo --no-index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM