简体   繁体   English

包含函数exec的python代码是否可以编译为可执行文件?

[英]Does a python code with the function exec in it get compiled to an executable?

I am new to python and I have never compiled python code to an executable file before. 我是python的新手,以前从未将python代码编译为可执行文件。 The languages I am well familiar with are C, C++, and Java and I have never come across a language that lets you modify the code from within itself, like Python which uses has the method exec . 我熟悉的语言是C,C ++和Java,但我从来没有遇到过一种可以让您从内部修改代码的语言,例如使用exec方法的Python。

For the following code, 对于以下代码,

a = 500
code  = raw_input() 
exec (code)

When I give the input as, print (a) the program displays the value in a . 当我输入为as时, print (a)程序在a显示值。 So this means the variable a comes within the scope of the code . 因此,这意味着变量a属于代码范围

I don't understand what would happen if we try to convert the python code to an executable using a program like py2exe . 我不明白如果我们尝试使用py2exe类的程序将python代码转换为可执行文件,将会发生什么。 Will the method exec still work? 方法exec仍然可以使用吗? If it does work, does py2exe bring the entire Python compiler and interpreter with it when the program gets compiled? 如果它能正常工作,那么在程序被编译时py2exe带来整个Python编译器和解释器吗?

py2exe never compiles Python code into native executables; py2exe 永远不会将Python代码编译为本地可执行文件。 it bundles up a Python interpreter into an executable, always. 它总是将Python解释器捆绑为可执行文件。 This is likewise true of freeze , cx_Freeze and every other tool offering similar functionality while supporting the full Python language rather than a limited subset thereof. 这是真正的同样freezecx_Freeze和所有其他工具提供类似的功能,同时支持完整的Python语言,而不是它的有限的子集。

Thus, exec , eval and similar constructs are available without needing additional facilities. 因此, execeval和类似的构造可用,而无需其他设施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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