编译一个.pyw文件,所以它可以运行像.pyc没有控制台 [英] Compile a .pyw file so it can be run like .pyc without console

查看:189
本文介绍了编译一个.pyw文件,所以它可以运行像.pyc没有控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译一个pyw文件到pyc没有控制台。当我尝试直接编译它,结果是一个pywc文件,但似乎pythonw.exe没有注册该扩展作为其中的一个像python.exe的pyc的文件。



结果当然是没有双击处理程序,当你尝试执行,或者如果你将扩展名改为pyc,你得到控制台。



有人知道解决这个问题吗?

感谢!



更新:
因为运行这个通过execfile或双击Windows中的图标soesn't生成此文件的编译版本,我在命令行启动python,然后:

  import py_compile 
py_compile.compile([FileName] .pyw)

这是我从.pywc扩展名。
我的Python版本是2.5.4

解决方案

(在OP的帖子稍作讨论)



如果你需要的是以* .pyc或* .pyo的形式为客户端或用户提供编译的应用程序,并且避免在执行时打开控制台,一个好的选择是离开



让我们假设主应用程序在 main.pyc 中,和进入点 main ,我们调用包装器 top.pyw ,它通常看起来像:

 #top.pyw file 
import main
if __name__ ==__main__:
import sys
main.main(* sys.argv [1:])

文件将如下所示:

 #main.py文件

主模块文档(可选)

#import modules

#函数和类定义

def main(* argv):
#解析选项(可选)
import optparse
parser = optparse.OptionParser(usage =%prog [< options>] \\\
+ __doc__)
parser.add_option 。
parser.add_option(...)
opt,args = parser.parse_args(list(argv))
#调用相应的函数:
my_function )

注意,* .pyc往往是特定版本。您可以检查上述解决方案是否与 pyInstaller和类似的独立分发方法兼容



Edit => 事实上,如果你使用pyInstaller,你可以简单地包含所有的脚本,独立于Python安装,并且在创建规​​范时不使用控制台( -w 选项)启动。你甚至不需要使用包装器或更改您的扩展。虽然它会是一个更大的文件,这可能是你正在寻找。



最后,只是为了防止你的头向:如果你不想要某人否则从编译的字节码提取源代码,不要忘记,你将需要额外的预防措施。这被称为代码混淆,并且在SO上有其他线程(例如这一个)。如果你只想出一个新的问题,如果你只是想出一个新的问题,那么这些事情可以快速变化。


I'm trying to compile a pyw file into a pyc without console. When I try a straight compile with it, the result is a pywc file, but it seems that pythonw.exe doesn't register that extension as one of it's files like python.exe does for a pyc.

The result of course is that it has no double click handler when you try to just execute, or if you change the extension to pyc, you get the console.

Does anybody know a way around this issue? Something to the affect of a .pyc with no console?

Thanks!

Update: Since running this through execfile or by double clicking on the icon in windows soesn't generate a compiled version of this file, I start python in command line, then :

import py_compile
py_compile.compile("[FileName].pyw")

This is where I get the .pywc extension from. My Python version is 2.5.4

解决方案

(following a little discussion in the OP's post)

If what you need is provide clients or users with the compiled application in form of *.pyc or *.pyo, and avoid opening the console at the execution, a good option is to leave a *.pyw wrapper that calls the main application.

Let's assume the main application is in main.pyc, and the entry point main, and let's call the wrapper top.pyw, which would typically look like:

# top.pyw file
import main
if __name__ == "__main__":
    import sys
    main.main(*sys.argv[1:])

Your main.py file would then look like this:

# main.py file
"""
Main module documentation (optional)
"""
# import modules

# function and class definitions

def main(*argv):
    # Parses the options (optional)
    import optparse
    parser = optparse.OptionParser(usage="%prog [<options>]\n" + __doc__)
    parser.add_option(...)
    parser.add_option(...)
    opt, args = parser.parse_args(list(argv))
    # Calls the appropriate function:
    my_function(...)

Note also that *.pyc tend to be version-specific. You can check whether the solution above would be compatible with pyInstaller and similar "independent" distribution methods.

Edit => In fact, if you use pyInstaller, you can simply include all your scripts and produce an executable which will be independent of the Python installation, and starts with no console (-w option when you create the specs). You don't even need to use a wrapper or change your extensions. While it will be a larger file, that could be what you were looking for.

Finally, just in case that's where you are headed: if you don't want someone else to extract source code from the compiled bytecode, don't forget that you will need additional precautions. This is called code obfuscation and there are other threads on SO about that (for example this one). Don't hesitate to post a new question if you only come up with old answers on that one, those things can change fast.

这篇关于编译一个.pyw文件,所以它可以运行像.pyc没有控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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