Python之exec()/compile()方法使用

# Python内置函数exec()可以用来执行Python代码
# 或内置函数compile()编译的代码对象


# exec程序中可写入python语法格式的代码,并直接输出。
exec('print("Hello World!")')

# compile(source, filename, mode[, flags[, dont_inherit]])
# 中文说明:将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。
# 参数source:字符串或者AST(Abstract Syntax Trees)对象。
# 参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。
# 参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。
# 参数flag和dont_inherit:这两个参数暂不介绍,可选参数。

code = "for i in range(0, 10): print(i)"

cmpcode = compile(code, '', 'exec')

exec(cmpcode)
# Python内置函数exec()可以用来执行Python代码
# 或内置函数compile()编译的代码对象


# exec程序中可写入python语法格式的代码,并直接输出。
exec('print("Hello World!")')

# compile(source, filename, mode[, flags[, dont_inherit]])
# 中文说明:将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。
# 参数source:字符串或者AST(Abstract Syntax Trees)对象。
# 参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。
# 参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。
# 参数flag和dont_inherit:这两个参数暂不介绍,可选参数。

code = "for i in range(0, 10): print(i)"

cmpcode = compile(code, '', 'exec')

exec(cmpcode)


运行结果

这里写图片描述

本文转载自:https://blog.csdn.net/qq_878799579/article/details/74200485 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR: Command errored out with exit status 1: command: /Users/kangjiechen/Desktop/coding/smpl/venv/bin/python /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /var/folders/4m/y5lvbvrs3ggg53z8q9bgpl5h0000gn/T/tmpLgjXJZ cwd: /private/var/folders/4m/y5lvbvrs3ggg53z8q9bgpl5h0000gn/T/pip-install-kHCLqT/opencv-python Complete output (22 lines): Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 280, in <module> main() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 263, in main json_out['return_val'] = hook(**hook_input['kwargs']) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 114, in get_requires_for_build_wheel return hook(config_settings) File "/private/var/folders/4m/y5lvbvrs3ggg53z8q9bgpl5h0000gn/T/pip-build-env-JgEI3r/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 146, in get_requires_for_build_wheel return self._get_build_requires(config_settings, requirements=['wheel']) File "/private/var/folders/4m/y5lvbvrs3ggg53z8q9bgpl5h0000gn/T/pip-build-env-JgEI3r/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 127, in _get_build_requires self.run_setup() File "/private/var/folders/4m/y5lvbvrs3ggg53z8q9bgpl5h0000gn/T/pip-build-env-JgEI3r/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 243, in run_setup self).run_setup(setup_script=setup_script) File "/private/var/folders/4m/y5lvbvrs3ggg53z8q9bgpl5h0000gn/T/pip-build-env-JgEI3r/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 142, in run_setup exec(compile(code, __file__, 'exec'), locals()) File "setup.py", line 448, in <module> main() File "setup.py", line 99, in main % {"ext": re.escape(sysconfig.get_config_var("EXT_SUFFIX"))} File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 210, in escape s = list(pattern) TypeError: 'NoneType' object is not iterable ---------------------------------------- ERROR: Command errored out with exit status 1: /Users/kangjiechen/Desktop/coding/smpl/venv/bin/python /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /var/folders/4m/y5lvbvrs3ggg53z8q9bgpl5h0000gn/T/tmpLgjXJZ Check the logs for full command output.
07-21

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值