How do I execute a string containing Python code in Python?

后端 未结 14 2673
一向
一向 2020-11-22 02:42

How do I execute a string containing Python code in Python?

14条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 02:48

    It's worth mentioning, that' exec's brother exist as well called execfile if you want to call a python file. That is sometimes good if you are working in a third party package which have terrible IDE's included and you want to code outside of their package.

    Example:

    execfile('/path/to/source.py)'

    or:

    exec(open("/path/to/source.py").read())

提交回复
热议问题