简体   繁体   English

将python脚本+模块捆绑/执行到远程计算机

[英]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; 目标环境如下所示:

  • The machines will have base python run time installed 这些机器将安装基本的python运行时
  • I will have a SSH account; 我将有一个SSH帐户; I can login or execute commands remotely using ssh 我可以使用ssh远程登录或执行命令
  • i can copy files (scp) into my home dir 我可以将文件(scp)复制到我的主目录中
  • I am NOT allowed to install any thing on the machine; 我不允许在机器上安装任何东西; the machines may not even have access to Internet 这些机器甚至可能无法访问Internet
  • my scripts may use some 'exotic' python modules -- most likely they won't be present in the target machine 我的脚本可能使用了一些“外来的” python模块-很可能它们不会出现在目标计算机中
  • after the audit, my home directory will be nuked from the machine (leave no trace) 审核后,我的主目录将从计算机中删除(不留任何痕迹)

So what I like to do is: 所以我想做的是:

  • copy a directory structure of python scripts + modules to remote machine (say in /home/audituser/scripts). 将python脚本+模块的目录结构复制到远程计算机(例如/ home / audituser / scripts中)。 And modules can be copied into /home/audituser/scripts/pythhon_lib) 并且可以将模块复制到/ home / audituser / scripts / pythhon_lib)
  • then execute a script (say /home/audituser/scripts/myscript.py). 然后执行一个脚本(例如/home/audituser/scripts/myscript.py)。 This script will need to resolve all modules used from 'python_lib' sub directory. 该脚本将需要解析“ python_lib”子目录中使用的所有模块。

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 ,使用pipvirtualenv副本在其中安装任何第三方模块,在其中构建脚本,然后将整个环境复制到每台远程机器。

There are three things that make it potentially non-trivial: 有三件事使它可能变得不平凡:

  • If the remote machines don't (and can't) have virtualenv installed, you need to do one of the following: 如果远程计算机没有(也不能)安装virtualenv ,则需要执行以下操作之一:
    • In many cases, just copying a --relocatable environment over just works. 在许多情况下,只需复制--relocatable环境中通过只是工作。 See the documentation section on "Making Environments Relocatable". 请参阅“使环境可重定位”的文档部分。
    • You can always bundle 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. 这将使用户帐户处于永久更改状态。 (But fortunately, your user account is going to be nuked, so who cares?) (但幸运的是,您的用户帐户将被删除,所以谁在乎?)
    • You can write your own manual bootstrapping. 您可以编写自己的手动引导程序。 See the section on "Creating Your Own Bootstrap Scripts". 请参阅“创建自己的引导脚本”部分。
    • By default, you get a lot more than you need—the Python executable, the standard library, etc. 默认情况下,您可以获得比所需更多的东西-Python可执行文件,标准库等。
    • If the machines aren't identical, this may not work, or at least might not be as efficient. 如果机器不同,则可能无法正常工作,或者至少效率不高。
    • Even if they are, you're still often making your bundle orders of magnitude bigger. 即使它们是,您仍然经常使束的数量级更大。
    • See the documentation sections on Using Virtualenv without bin/python, --system-site-packages, and possibly bootstrapping. 请参阅有关在不使用bin / python的情况下使用Virtualenv的文档部分,--system-site-packages以及可能的引导程序。
  • If any of the Python modules you're installing also need C libraries (eg, libxml2 for lxml ), virtualenv doesn't help with that. 如果您要安装的任何Python模块也需要C库(例如, libxml2 for lxml ),则virtualenv对此无济于事。 In fact, you will need the C libraries to be almost exactly the same (same path, compatible version). 实际上,您将需要C库几乎完全相同(相同路径,兼容版本)。

Three other alternatives: 其他三种选择:

  • If your needs are simple enough (or the least-simple parts involve things that 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等操作,然后就完成了。
  • Just because you don't need a full app-distribution system doesn't mean you can't use one. 仅仅因为您不需要一个完整的应用程序分发系统,并不意味着您不能使用一个系统。 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. py2apppy2execx_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.

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