python 编译一个 .pyw 文件,这样它就可以像 .pyc 一样在没有控制台的情况下运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1690331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:52:13  来源:igfitidea点击:

Compile a .pyw file so it can be run like .pyc without console

pythoncompilation

提问by Fry

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.

我正在尝试将一个 pyw 文件编译成一个没有控制台的 pyc。当我尝试用它直接编译时,结果是一个 pywc 文件,但似乎 pythonw.exe 没有注册该扩展名,因为它的文件之一就像 python.exe 为 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.

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

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

有人知道解决这个问题的方法吗?没有控制台的 .pyc 有什么影响?

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 :

更新:由于通过 execfile 或双击 Windows 中的图标运行此文件,因此不会生成此文件的编译版本,因此我在命令行中启动 python,然后:

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

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

这是我从那里获得 .pywc 扩展名的地方。我的 Python 版本是 2.5.4

回答by RedGlyph

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

(在 OP 的帖子中进行了一些讨论之后)

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.

如果您需要以 *.pyc 或 *.pyo 的形式向客户端或用户提供已编译的应用程序,并且避免在执行时打开控制台,那么一个不错的选择是留下一个调用主应用程序的 *.pyw 包装器。

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:

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

# 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 文件将如下所示:

# 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.

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

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 (-woption 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.

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

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 obfuscationand 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.

最后,以防万一,这就是您要去的地方:如果您不希望其他人从已编译的字节码中提取源代码,请不要忘记您将需要额外的预防措施。这称为代码混淆,并且在 SO 上还有其他线程(例如这个)。如果您只针对该问题提出旧答案,请不要犹豫发布新问题,这些问题可能会很快发生变化。

回答by Ben

The interpreter that reads .pywfiles is also capable of reading a python compiled .pycformatted file. Simply rename your .pycfile to .pyw. It will run (although it will still be version specific), and you won't get the ugly console.

读取.pyw文件的解释器也能够读取 python 编译的.pyc格式化文件。只需将您的.pyc文件重命名为.pyw. 它将运行(尽管它仍然是特定于版本的),并且您不会得到丑陋的控制台。

回答by Todd

You can use the FTYPE and ASSOC commands to associate .pywc files with pythonw.exe.

您可以使用 FTYPE 和 ASSOC 命令将 .pywc 文件与 pythonw.exe 相关联。

ASSOC is used to associate an extension with a file type, and FTYPE associates a file type with the command used to open it. The following commands show existing assocations on my machine:

ASSOC 用于将扩展名与文件类型相关联,FTYPE 将文件类型与用于打开它的命令相关联。以下命令显示我机器上的现有关联:

C:\Python26>assoc .py
.py=Python.File

C:\Python26>assoc .pyc
.pyc=Python.CompiledFile

C:\Python26>assoc .pyw
.pyw=Python.NoConFile

C:\Python26>assoc .pywc
File association not found for extension .pywc

These commands show what programs the extensions are associated with through their file types:

这些命令通过文件类型显示扩展与哪些程序相关联:

C:\Python26>ftype Python.CompiledFile
Python.CompiledFile="C:\Python26\python.exe" "%1" %*

C:\Python26>ftype Python.NoConFile
Python.NoConFile="C:\Python26\pythonw.exe" "%1" %*

So it looks like you could just associate .pywc files with Python.NoConFile like this:

所以看起来你可以像这样将 .pywc 文件与 Python.NoConFile 相关联:

assoc .pywc=Python.NoConFile

回答by Kuldip Chaudhari

Just rename the [fileName].pywc to [fileName].pyw and open it. It works it did for me. Also the code is not visible when you try to open it for editing it opens like an exe file in notepad.

只需将 [fileName].pywc 重命名为 [fileName].pyw 并打开它。它对我有用。此外,当您尝试打开它进行编辑时,该代码不可见,它会像记事本中的 exe 文件一样打开。