[英]bundling/executing python script + modules to a remote machine
I have looked into other python module distribution questions. 我研究了其他python模块分发问题。 My need is a bit different (I think!, I am python newbie+)
我的需求有些不同(我认为!,我是python newbie +)
I have a bunch of python scripts that I need to execute on remote machines. 我有一堆需要在远程计算机上执行的python脚本。 Here is what the target environment looks like;
目标环境如下所示:
So what I like to do is: 所以我想做的是:
is this possible? 这可能吗? or is there a better way of doing this?
还是有更好的方法呢? I guess what I am looking is to 'relocate' the 3rd party modules into the scripts dir.
我想我正在寻找的是将第三方模块“重新定位”到脚本目录中。
thanks in advance! 提前致谢!
Are the remote machines the same as each other? 远程机器彼此相同吗? And, if so, can you set up a development machine that's effectively the same as the remote machines?
而且,如果可以,您是否可以设置与远程计算机实际上相同的开发计算机?
If so, virtualenv
makes this almost trivial. 如果是这样,
virtualenv
几乎可以做到这一点。 Create a virtualenv
on your dev machine, use the virtualenv
copy of pip
to install any third-party modules into it, build your script within it, then just copy that entire environment to each remote machine. 在开发机器上创建
virtualenv
,使用pip
的virtualenv
副本在其中安装任何第三方模块,在其中构建脚本,然后将整个环境复制到每台远程机器。
There are three things that make it potentially non-trivial: 有三件事使它可能变得不平凡:
virtualenv
installed, you need to do one of the following: virtualenv
,则需要执行以下操作之一:
--relocatable
environment over just works. --relocatable
环境中通过只是工作。 See the documentation section on "Making Environments Relocatable". virtualenv
itself, and pip install --user virtualenv
(and, if they don't even have pip
, a few steps before that) on each machine. virtualenv
本身捆绑在一起,然后在每台计算机上pip install --user virtualenv
(如果他们甚至没有pip
,请在此之前执行几个步骤)。 This will leave the user account in a permanently-changed state. libxml2
for lxml
), virtualenv
doesn't help with that. libxml2
for lxml
),则virtualenv
对此无济于事。 In fact, you will need the C libraries to be almost exactly the same (same path, compatible version). Three other alternatives: 其他三种选择:
virtualenv
doesn't help with, like installing libxml2
), it may be easier to just bundle the .egg/.tgz/whatever files for third-party modules, and write a script that does a pip install --user
and so on for each one, and then you're done. virtualenv
问题,例如安装libxml2
),那么将.egg / .tgz /任何文件捆绑到第三方模块中可能会更容易,然后编写一个脚本,对每个脚本执行pip install --user
等操作,然后就完成了。 py2app
, py2exe
, cx_freeze
, etc. aren't all that complicated, especially in simple cases, and having a click-and-go executable to copy around is even easier than having an explicit environment. py2app
, py2exe
, cx_freeze
等并不那么复杂,尤其是在简单的情况下,拥有一个cx_freeze
可执行文件进行复制比拥有一个明确的环境更加容易。 zc.buildout
is an amazingly flexible and manageable tool that can do the equivalent of any of the three alternatives. zc.buildout
是一种非常灵活且易于管理的工具,可以完成三种选择中的任何一种。 The main downside is that there's a much, much steeper learning curve. You can use virtualenv to create a self-contained environment for your project. 您可以使用virtualenv为您的项目创建一个独立的环境。 This can house your own script, as well as any dependency libraries.
这可以容纳您自己的脚本以及任何依赖库。 Then you can make the env relocatable (
--relocatable
), and sync it over to the target machine, activate it, and run your scripts. 然后,您可以使环境可重定位(
--relocatable
),并将其同步到目标计算机,激活它并运行脚本。
If these machines do have network access (not internet, but just local network), you can also place the virtualenv on a shared location and activate from there. 如果这些计算机确实具有网络访问权限(不是Internet,而是局域网),则您也可以将virtualenv放在共享位置并从那里激活。
It looks something like this: 看起来像这样:
virtualenv --no-site-packages portable_proj
cd portable_proj/
source bin/activate
# install some deps
pip install xyz
virtualenv --relocatable .
Now portable_proj
can be disted to other machines. 现在
portable_proj
可以disted到其他机器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.