简体   繁体   English

每次对脚本进行更改时,都需要在终端中重新启动python

[英]Need to restart python in Terminal every time a change is made to script

Every time I make a change to a python script I have to reload python and re-import the module. 每次我对python脚本进行更改时,都必须重新加载python并重新导入模块。 Please advise how I can modify my scripts and run then without having to relaunch python in the terminal. 请告诉我如何修改我的脚本然后运行,而无需在终端重新启动python。

Thanks. 谢谢。

I've got a suggestion, based on your comment describing your work flow: 根据你的评论描述你的工作流程,我有一个建议:

first, i run python3.1 in terminal second, i do "import module" then, i run a method from the module lets say "module.method(arg)" every time, i try to debug the code, i have to do this entire sequence, even though the change is minor. 首先,我在终端秒运行python3.1,我做“导入模块”然后,我从模块运行一个方法,每次都说“module.method(arg)”,我尝试调试代码,我必须做整个序列,即使变化很小。 it is highly inefficient 这是非常低效的

Instead of firing up the interactive Python shell, make the module itself executable. 而不是启动交互式Python shell,使模块本身可执行。 The easiest way to do this is to add a block to the bottom of the module like so: 最简单的方法是在模块底部添加一个块,如下所示:

if __name__ == '__main__':
    method(arg) # matches what you run manually in the Python shell

Then, instead of running python3.1, then importing the module, then calling the method, you can do something like this: 然后,不是运行python3.1,而是导入模块,然后调用方法,你可以这样做:

python3.1 modulename.py

and Python will run whatever code is in the if __name__ == '__main__' block. Python将运行if __name__ == '__main__'块中的任何代码。 But that code will not be run if the module is imported by another Python module. 但是,如果模块由另一个Python模块导入,则不会运行该代码。 More information on this common Python idiom can be found in the Python tutorial . 有关这种常见Python习惯用法的更多信息可以在Python教程中找到。

The advantage of this is that when you make a change to your code, you can usually just re-run the module by pressing the up arrow and hitting enter. 这样做的好处是,当您对代码进行更改时,通常可以通过按向上箭头并按Enter键重新运行模块。 No messy reloading necessary. 不需要杂乱的重装。

如果它只是一些正在改变的模块,你可以在脚本中调用reload(module)

你的意思是你直接在交互式python中输入脚本,或者你是通过运行python myscript.py类的东西从终端执行.py文件的?

You can use reload to re-import a module. 您可以使用reload来重新导入模块。 I use it frequently when using the interactive mode to debug code. 我在使用交互模式调试代码时经常使用它。

However, from a higher level, I would be hesitant to use that in a production version of a program. 但是,从更高的层面来看,我会犹豫是否在程序的生产版本中使用它。 Unless you will have very strict control over how sub-modules are changing, it would not be hard to have the reloaded module change in some way that breaks your program. 除非您对子模块的更改方式有非常严格的控制,否则重新加载的模块会以某种方式更改会破坏您的程序并不困难。 Unless your program really needs 100% up-time, it would make sense to stop it and start it again when there is some kind of version change. 除非您的程序确实需要100%的正常运行时间,否则在有某种版本更改时停止并再次启动它是有意义的。

I found a bypass online. 我在网上找到了绕道。 Magic commands exist. 魔术命令存在。 It works like a charm for me. 它对我来说就像一个魅力。 The Reload option didn't work for me. 重新加载选项对我不起作用。 Got it from here and point 3 of this link. 这里得到它和这个链接的第3点。

Basically all you have to do is the following: and changes you make are reflected automatically after you save. 基本上您只需要执行以下操作:保存后,您所做的更改会自动反映出来。

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: Import MODULE

In [4]: my_class = Module.class()
        my_class.printham()
Out[4]: ham

In [5]: #make changes to printham and save
In [6]: my_class.printham() 
Out[6]: hamlet

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

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