Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以访问已经可执行的Python脚本的代码吗?_Python_Python 3.x - Fatal编程技术网

我可以访问已经可执行的Python脚本的代码吗?

我可以访问已经可执行的Python脚本的代码吗?,python,python-3.x,Python,Python 3.x,例如,我运行了script test.py,并意外地更改了可执行文件,但该文件中的脚本继续工作。 在顶部,我可以看到执行“python3test.py”的是什么,但它并不完全正确。 我可以访问更改后执行的代码吗?恢复源代码的一种可能方法是使用工具和 Pyrasite用于将代码注入Python进程,inspect用于检索模块、类、方法、函数、回溯、帧对象和代码对象的源代码 例如。 初始文件:foobar.py import time def test1(): print "test1"

例如,我运行了script test.py,并意外地更改了可执行文件,但该文件中的脚本继续工作。 在顶部,我可以看到执行“python3test.py”的是什么,但它并不完全正确。
我可以访问更改后执行的代码吗?

恢复源代码的一种可能方法是使用工具和

Pyrasite用于将代码注入Python进程,inspect用于检索模块、类、方法、函数、回溯、帧对象和代码对象的源代码

例如。 初始文件:foobar.py

import time

def test1():
    print "test1"

if __name__ == "__main__":
    while (1):
        time.sleep(1)
        test1()
运行并修改文件(我通过“test2”更改了打印的字符串):

它仍在打印
test1
,如果使用pyrasite并进行检查,则可以看到它正在运行的函数的源代码

$ pyrasite-shell 6243
Pyrasite Shell 2.0
Connected to 'python foobar.py'
Python 2.7.9 (default, Jun 29 2016, 13:08:31) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(DistantInteractiveConsole)

>>> import inspect
>>> inspect.getsource(test1)
'def test1():\n\tprint "test1"\n'

另外,如果您也删除了该文件,它也可以工作。

您的答案很好,我只是有一个小问题,当我尝试调用inspect(example_function)(是的,我对此名称感到抱歉)时,其他地方定义的函数被称为('/usr/local/lib/python3.5 dist packages/pytg/utils.py),而不是在源文件中,我对@coroutine def example_function(sender)的定义:我想你应该导入这个模块
$ pyrasite-shell 6243
Pyrasite Shell 2.0
Connected to 'python foobar.py'
Python 2.7.9 (default, Jun 29 2016, 13:08:31) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(DistantInteractiveConsole)

>>> import inspect
>>> inspect.getsource(test1)
'def test1():\n\tprint "test1"\n'