Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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 - Fatal编程技术网

python模块何时重新编译?

python模块何时重新编译?,python,Python,如果主python程序首先初始化一组模块,然后等待运行它们的def run函数,那么如果发生更改,它将在何时重新编译模块 考虑下面的例子 主脚本 假设mod_a.Run和mod_b.Run需要一些时间运行 我开始主脚本 当mod_a.Run正在运行时,我在mod_c.Run中做了一个更改并删除mod_c.pyc文件 当主脚本调用mod_c.Run时,它会重新编译并合并更改吗?编译在导入时进行。一旦你导入它,你甚至可以删除源文件,它仍然可以工作 (2) % ls foo.py (2) % cat

如果主python程序首先初始化一组模块,然后等待运行它们的def run函数,那么如果发生更改,它将在何时重新编译模块

考虑下面的例子

主脚本

假设mod_a.Run和mod_b.Run需要一些时间运行

我开始主脚本

当mod_a.Run正在运行时,我在mod_c.Run中做了一个更改并删除mod_c.pyc文件


当主脚本调用mod_c.Run时,它会重新编译并合并更改吗?

编译在导入时进行。一旦你导入它,你甚至可以删除源文件,它仍然可以工作

(2) % ls
foo.py
(2) % cat foo.py
def display():
    print ("Hello, world")
(2) % python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
[3.5.2] >>> import foo # Imports foo
[3.5.2] >>> # Now the file is also compiled and stored in __pycache__
[3.5.2] ...
[3.5.2] >>> import shutil
[3.5.2] >>> shutil.rmtree("__pycache__")
[3.5.2] >>> import os
[3.5.2] >>> os.unlink("foo.py")
[3.5.2] >>> # Hit Ctrl-Z here to suspend the interpreter
zsh: suspended  python
-148-(3) % ls # No files here. Everything is deleted
(3) % fg
[3]  - continued  python


[3.5.2] >>> foo.display()
Hello, world
[3.5.2] >>> # Still works.

编译在导入时进行。一旦你导入它,你甚至可以删除源文件,它仍然可以工作

(2) % ls
foo.py
(2) % cat foo.py
def display():
    print ("Hello, world")
(2) % python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
[3.5.2] >>> import foo # Imports foo
[3.5.2] >>> # Now the file is also compiled and stored in __pycache__
[3.5.2] ...
[3.5.2] >>> import shutil
[3.5.2] >>> shutil.rmtree("__pycache__")
[3.5.2] >>> import os
[3.5.2] >>> os.unlink("foo.py")
[3.5.2] >>> # Hit Ctrl-Z here to suspend the interpreter
zsh: suspended  python
-148-(3) % ls # No files here. Everything is deleted
(3) % fg
[3]  - continued  python


[3.5.2] >>> foo.display()
Hello, world
[3.5.2] >>> # Still works.

这回答了你的问题吗?简而言之,不,在将对原始py/pyc文件所做的任何更改合并到当前运行的环境中之前,您的代码必须显式重新加载模块。这是否回答了您的问题?简而言之,不,在将对原始py/pyc文件所做的任何更改合并到当前运行的环境中之前,您的代码必须显式重新加载模块。由于字节码已加载到RAM中,程序是否继续运行?对此的任何进一步解释都将不胜感激-我假设删除pyc文件后一切都会爆炸。是的。导入后,代码将被编译并加载到内存中。之后,当前进程不再需要源代码。编译期间发生的语法错误在导入时引发。当你真的运行东西的时候,程序会继续运行吗?因为字节码已经加载到RAM中了吗?对此的任何进一步解释都将不胜感激-我假设删除pyc文件后一切都会爆炸。是的。导入后,代码将被编译并加载到内存中。之后,当前进程不再需要源代码。编译期间发生的语法错误在导入时引发。当你真的运行东西的时候。