如何使用 py2exe 制作 Python API? [英] How to make Python API using py2exe?

查看:65
本文介绍了如何使用 py2exe 制作 Python API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 py2exe(或类似工具)编译"一个 Python 脚本,然后允许用户访问以修改顶级 Python 脚本?或者可能将编译后的模块导入到它们的普通 Python 脚本中?我希望能够为某些客户分发简单的安装程序,但允许其他客户通过创建与已安装的框架模块(如 API)一起使用的脚本来构建已安装的版本.

Is it possible to "compile" a Python script with py2exe (or similar) and then allow the user access to modify the top-level Python scripts? Or possibly import the compiled modules into their normal Python scripts? I'm looking for the ability to distribute an easy installer for some customers, but allow other customers to build upon that installed version by creating their own scripts that work with the installed framework modules, like an API.

我尝试使用 py2exe 导入我放在dist"目录中的文件,但它抱怨它们没有被冻结.为什么不能混合使用冻结二进制模块和解释模块?

I have tried to use py2exe to import files that I have placed in the "dist" directory, but it complains that they aren't frozen. Why can't it use a mix of frozen binary modules and interpreted modules?

我使用 py2exe 的原因是因为我有一些麻烦的库(paramiko/pycrypto,以及一些内部开发的),我不想要求我的客户在这些安装中跋涉.我也不希望他们可以公开访问我的框架文件.我知道他们可以反向编译 py2exe 对象,但他们将不得不修改框架,这已经足够保护了.

The reason that I am using py2exe is because I have some troublesome libraries (paramiko/pycrypto, plus some internally developed ones) that I don't want to require my customers to trudge through those installations. I also don't want them to have open access to my framework files. I know that they can reverse-compile the py2exe objects, but they will have to work to modify the framework, which is good enough protection.

推荐答案

我想出了如何让它发挥作用.我将我的head"框架文件放在 setup.py 文件的includes"列表中.然后,我有一个编译运行器,它使用 imp 模块动态加载常规 Python 脚本,这些脚本调用该头框架文件.这正是我正在寻找的那种隐藏的框架,但可访问的 API.

I figured out how to get it to work. I placed my "head" framework file in the "includes" list in the setup.py file. Then, I have a compliled runner that uses the imp module to dynamically load regular Python scripts, and those scripts call upon that head framework file. This is exactly the kind of hidden framework, yet reachable API that I was looking for.

例如,假设我们有一个名为framework"的目录,其中包含一个包含所有 API 调用的主文件foo".py2exe setup.py 文件中的行如下所示:

For example, let's say we have a directory called "framework" with a master file "foo" that contains all of the API calls. The line in the py2exe setup.py file would look like this:

includes = ['framework.foo', 'some_other_module', 'etc']

然后我为这个运行脚本创建一个目标:

I then make a target for this runner script:

FrameworkTarget = Target(
    # what to build
    script = "run_framework.py",
    dest_base = "run_framework"   
    )

然后将目标添加到 setup.py 脚本中的 setup() 命令中:

Then add the target to the setup() command in the setup.py script among the other things:

console = [FrameworkTarget],

编译的运行器脚本从命令行传递测试套件"脚本的名称:

The compiled runner script is passed the name of the "test suite" script from the command line:

test_suite_name = sys.argv[1]
file_name = test_suite_name + ".py"
path_name = os.path.join(os.getcwd(), file_name)
print "Loading source %s at %s"%(file_name, path_name)
module = imp.load_source(file_name, path_name )

然后,在imp.load_source()命令调用的文件中,我有这个:

Then, in the file called by the imp.load_source() command, I have this:

import framework.foo

当我的包含中没有 'framework.foo' 时,它找不到 framework.foo 的编译版本.也许将来有人会发现这很有用.如果没有 Stackoverflow,我不知道我是否可以做一件有用的事情!

When I didn't have 'framework.foo' in my includes, it couldn't find the compiled version of framework.foo. Maybe someone will find this useful in the future. I don't know if I could do one useful thing without Stackoverflow!

这篇关于如何使用 py2exe 制作 Python API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆