如何离线安装软件包?
从 pypi 下载 python 包及其依赖项以便在另一台计算机上离线安装的最佳方法是什么?有没有简单的方法可以使用 pip 或 easy_install 来做到这一点?我正在尝试在未连接到互联网的 FreeBSD 机器上安装 requests 库。
What's the best way to download a python package and its dependencies from pypi for offline installation on another machine? Is there any easy way to do this with pip or easy_install? I'm trying to install the requests library on a FreeBSD box that is not connected to the internet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
在可以访问互联网的系统上
pip
download
命令可让您下载软件包而不安装它们:(在以前版本的 pip 中,拼写为
pip install --download -rrequirements.txt
.)在无法访问互联网的系统上
将下载的软件包复制到该系统中,然后您就可以用来
安装这些下载的模块,而无需访问网络。
On the system that has access to internet
The pip
download
command lets you download packages without installing them:(In previous versions of pip, this was spelled
pip install --download -r requirements.txt
.)On the system that has no access to internet
Copy over the downloaded packages to this system and then you can use
to install those downloaded modules, without accessing the network.
如果您想离线安装 python 库及其依赖项,请在具有相同操作系统、网络连接且安装了 python 的计算机上执行以下步骤:
requirements.txt
文件,其内容如下(这些是您想要下载的库):创建需求文件的一种选择是使用
pip freeze >requirements.txt
。这将列出您环境中的所有库。然后你可以进入requirements.txt
并删除不需要的。执行命令
mkdir driverhouse && pip download -rrequirements.txt -dwheelhouse
将库及其依赖项下载到目录wheelhouse
将requirements.txt复制到
wheelhouse
目录使用
tar -zcfwheelhouse.tar.gzwheelhouse
将wheelhouse存档到wheelhouse.tar.gz
< /p>,然后上传
wheelhouse.tar.gz
到您的目标计算机:执行
tar -zxfwheelhouse.tar.gz
解压文件执行
pip install -r wheelhouse/requirements.txt --no-index --find-links wheelhouse
安装库及其依赖项If you want install python libs and their dependencies offline, follow these steps on a machine with the same os, network connected, and python installed:
requirements.txt
file with content like this (these are the libraries you wish to download):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 torequirements.txt
and remove the unnecessary ones.Execute command
mkdir wheelhouse && pip download -r requirements.txt -d wheelhouse
to download libs and their dependencies to directorywheelhouse
Copy requirements.txt into
wheelhouse
directoryArchive wheelhouse into
wheelhouse.tar.gz
withtar -zcf wheelhouse.tar.gz wheelhouse
Then upload
wheelhouse.tar.gz
to your target machine:Execute
tar -zxf wheelhouse.tar.gz
to extract the filesExecute
pip install -r wheelhouse/requirements.txt --no-index --find-links wheelhouse
to install the libs and their dependencies如果包位于 PYPI 上,请将其及其依赖项下载到某个本地目录。
例如,
某些包可能必须手动归档到类似的 tarball 中。当我想要某个东西的更新(不太稳定)版本时,我经常这样做。有些软件包不在 PYPI 上,因此同样适用于它们。
假设您在
~/src/myapp
中有一个格式正确的 Python 应用程序。~/src/myapp/setup.py
将包含install_requires
列表,其中提及/pypi
目录中的一项或多项内容。像这样:如果您希望能够运行具有所有必要依赖项的应用程序,同时仍然对其进行黑客攻击,您将执行以下操作:
这样您的应用程序将直接从源目录执行。您可以破解一些东西,然后重新运行应用程序而无需重建任何内容。
如果您想将应用程序及其依赖项安装到当前的 python 环境中,您将执行以下操作:
在这两种情况下,如果
/pypi
目录。它不会尝试从互联网上随意安装丢失的东西。我强烈建议在活动的 虚拟环境以避免污染您的全局Python环境。这就是(virtualenv)几乎要走的路。切勿将任何东西安装到全局 Python 环境中。
如果您构建应用程序的计算机与要部署应用程序的计算机具有相同的架构,则只需将所有内容
easy_install
压缩到整个虚拟环境目录即可。不过,在打包之前,您必须使虚拟环境目录可重定位(请参阅 --relocatable 选项)。 注意:目标计算机需要安装相同版本的 Python,并且您的应用程序可能具有的任何基于 C 的依赖项也必须预先安装在那里(例如,如果您依赖于 PIL,然后必须预安装libpng、libjpeg等)。If the package is on PYPI, download it and its dependencies to some local directory.
E.g.
Some packages may have to be archived into similar looking tarballs by hand. 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.
Suppose you have a properly formed Python application in
~/src/myapp
.~/src/myapp/setup.py
will haveinstall_requires
list that mentions one or more things that you have in your/pypi
directory. Like so: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:
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:
In both cases, the build will fail if one or more dependencies aren't present in
/pypi
directory. It won't attempt to promiscuously install missing things from Internet.I highly recommend to invoke
setup.py develop ...
andeasy_install ...
within an active virtual environment to avoid contaminating your global Python environment. It is (virtualenv that is) pretty much the way to go. Never install anything into global Python environment.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).让我逐步完成该过程:
打开命令提示符或 shell 并执行以下命令:
假设你想要的包是
tensorflow
$ pip download tensorflow
现在,在目标计算机上,复制
packages
文件夹并应用以下命令注意,
tensorflow-xyz.whl
必须替换为所需包的原始名称。Let me go through the process step by step:
open up a command prompt or shell and execute the following command:
Suppose the package you want is
tensorflow
$ pip download tensorflow
Now, on the target computer, copy the
packages
folder and apply the following commandNote that the
tensorflow-xyz.whl
must be replaced by the original name of the required package.离线Python。为此,我使用 virtualenv (隔离的 Python 环境)
1) 安装 virtualenv
使用 pip 在线:
或使用 whl 离线:转到此 链接 ,下载最新版本(.whl 或tar.gz)并使用此命令安装它:
通过使用
--user
您不需要使用sudo pip...
。2) 在在线机器上使用 virtualenv
使用终端 cd 选择一个目录并运行此代码:
安装所有软件包后,您必须生成一个
requirements.txt
所以当您的 virtualenv处于活动状态,请写入打开一个新终端并创建另一个环境,例如
myenv2
。现在您可以转到离线文件夹,其中有
requirements.txt
和tranferred_packages
文件夹。使用以下代码下载软件包并将其全部放入tranferred_packages
文件夹中。将您的离线文件夹带到离线计算机,然后
离线文件夹中的内容 [requirements.txt , tranferred_packages {Flask-0.10.1.tar.gz, ...}]
检查您的包注释列表
:就像我们在 2017 年一样最好使用 python 3。您可以使用此命令创建 python 3 virtualenv。
offline python. for doing this I use virtualenv (isolated Python environment)
1) install virtualenv
online with pip:
or offline with whl: go to this link , download last version (.whl or tar.gz) and install that with this command:
by using
--user
you don't need to usesudo pip…
.2) use virtualenv
on online machine select a directory with terminal
cd
and run this code:after installing all the packages, you have to generate a
requirements.txt
so while your virtualenv is active, writeopen a new terminal and create another env like
myenv2
.now you can go to your offline folder where your
requirements.txt
andtranferred_packages
folder are in there. download the packages with following code and put all of them totranferred_packages
folder.take your offline folder to offline computer and then
what is in the folder offline [requirements.txt , tranferred_packages {Flask-0.10.1.tar.gz, ...}]
check list of your package
note: as we are in 2017 it is better to use python 3. you can create python 3 virtualenv with this command.
我有类似的问题。我必须以与 pypi 相同的方式安装它。
我做了以下事情:
创建一个目录来存储可以访问互联网的计算机中的所有包。
下载所有包到该路径
Tar 软件包目录并将其复制到无法访问互联网的计算机。然后做,
将 packages.tar.gz 复制到无法访问互联网的目标计算机中。
在无法访问 Internet 的计算机中,执行以下操作(假设您将 tarred 包复制到当前计算机中的 /path/to/package/)
并将以下内容粘贴到其中并保存。
最后,我建议您使用某种形式的
virtualenv
来安装软件包。您应该能够下载目录 /path/to/package/ 中的所有模块。
注意:我只这样做了,因为我无法添加选项或更改我们安装模块的方式。不然我就这么做了
I had a similar problem. And i had to make it install the same way, we do from pypi.
I did the following things:
Make a directory to store all the packages in the machine that have internet access.
Download all the packages to the path
Tar the packages directory and copy it to the Machine that doesn't have internet access. Then do,
Copy the packages.tar.gz into the destination machine that doesn't have internet access.
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)
and paste the following content inside and save it.
Finally, i suggest you use, some form of
virtualenv
for installing the packages.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
对于具有另一个 Python 版本的另一个平台
要下载另一个平台的 python 包,您需要将
--platform
参数[1] 与--only- 结合使用binary=:all:
参数。要同时定义目标系统的 Python 版本,请使用--python-version
参数。示例场景:您在 Windows 上,想要下载适用于使用 Python 3.9 的 Linux 系统的
numpy
。使用以下命令下载numpy
及其目标系统的所有依赖项:要在目标系统上安装
numpy
,请将下载的文件复制到其中并通过以下方式安装软件包:您还可以为这两个命令使用需求文件,而不是定义特定的包名称。例如,只需将包名称替换为
-rrequirements.txt
即可。[1] 查找正确的平台字符串可能很乏味。您可以在目标系统上执行以下命令来开始:
python -c“导入sysconfig; print(sysconfig.get_platform().replace('-','_').replace('.','_'))”
。对于 Linux,您最好在此处查找合适的平台字符串。
但最终,这完全取决于该软件包的维护者是否实际上为特定平台构建了它。您可以通过查看特定包的文件并检查轮子文件的后缀及其标签来查找,例如: numpy 文件。
For another platform with another Python version
To download python packages for another platform, you need the
--platform
parameter[1] in combination with the--only-binary=:all:
parameter. To also define the Python version of the target system, use the--python-version
parameter.Example scenario: You are on Windows and want to download
numpy
for a Linux system that uses Python 3.9. Use the following command to downloadnumpy
with all its dependencies for your target system:To install
numpy
on your target system, copy the downloaded files to it and install the package via:Instead of defining a particular package name, you can also use a requirements file for both commands. Just replace the package name by
-r requirements.txt
, for example.[1] It can be tedious to find the correct platform string. You can execute the following command on your target system to get a start:
python -c "import sysconfig; print(sysconfig.get_platform().replace('-','_').replace('.','_'))"
.For Linux, you better look here for a suitable platform string.
But in the end, it all depends on if the maintainers of the package actually built it for a particular platform. You can look that up by viewing the files of the particular package and by checking the suffixes of the wheel files and their tags, e. g.: numpy files.
下载 tarball,将其传输到您的 FreeBSD 计算机并解压,然后运行
python setup.py install
就完成了!编辑:除此之外,您现在还可以使用 pip 安装 tarball。
Download the tarball, transfer it to your FreeBSD machine and extract it, afterwards run
python setup.py install
and you're done!EDIT: Just to add on that, you can also install the tarballs with pip now.
使用
wheel
编译的包。捆绑:
复制 tarball 并安装:
注意
wheel
二进制包不能跨机器。更多参考。这里: https://pip.pypa.io/en/stable/user_guide/ #安装包
Using
wheel
compiled packages.bundle up:
copy tarball and install:
Note
wheel
binary packages are not across machines.More ref. here: https://pip.pypa.io/en/stable/user_guide/#installation-bundles
对于 Windows,我使用了以下内容
Internet 连接
1.创建任意名称的目录。我使用“repo”创建了
2.使用以下命令下载库(它将下载而不是安装)
pip download libraray_name -d "C:\repo"
然后你会发现多个.whl扩展文件
复制requirements.txt中的所有文件名

没有互联网连接
您可以阅读详细博客链接
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"
Then you will find multiple .whl extension files
copy all the filename in requirements.txt

No Internet Connection
You can read the detailed blog Link
作为@chaokunyang 的继续回答,我想将我编写的完成工作的脚本放在这里:
“requirements.txt”文件
打包端:文件名:“create-offline-python3.6-dependency-repository.sh”
安装端:文件名:“install-python-libraries-offline.sh”
As a continues to @chaokunyang answer, I want to put here the script I write that does the work:
"requirements.txt" file
Packer side: file name: "create-offline-python3.6-dependencies-repository.sh"
Installer side: file name: "install-python-libraries-offline.sh"
对于 Pip 8.1.2,您可以使用
pip download -r requ.txt
将软件包下载到本地计算机。For Pip 8.1.2 you can use
pip download -r requ.txt
to download packages to your local machine.从 Pypi 下载轮文件(例如 dlb-0.5.0-py3-none-any.whl)并
Download the wheel file (for example dlb-0.5.0-py3-none-any.whl) from Pypi and
我发现了一种更简单的方法,根本不涉及 pip。
只需将 site-packages 文件夹从在线计算机复制到离线计算机即可。
我在
%USERPROFILE%\AppData\Local\Programs\Python\Python312\Lib\site-packages\
中找到了我的。有关位置,请参阅 pip 在哪里安装其软件包?。然后,我将
site-packages
文件夹压缩到 site-packages.zip,进行传输,然后将所有丢失的包复制到另一台计算机的 site-packages 文件夹中。之后就成功了。最好确保两台计算机上的 python 版本相同。
I found a much simpler way that does not involve pip at all.
Just copy the site-packages folder from your online machine to the offline one.
I found mine in
%USERPROFILE%\AppData\Local\Programs\Python\Python312\Lib\site-packages\
. See Where does pip install its packages? for the location.I then zipped the
site-packages
folder to site-packages.zip, transferred it, and then copied any missing packages to the other computer's site-packages folder. After that it worked.Best make sure your python versions are the same on both computers.