简体   繁体   English

进行更改后,从终端重新运行python程序

[英]Rerun python program from terminal after making changes

It is very nice and easy to run Python from the command line. 从命令行运行Python非常好且容易。 Especially for testing purposes. 特别用于测试目的。 The only drawback is that after making a change in the script, I have to restart Python, do all the imports over again, create the objects and enter the parameters. 唯一的缺点是在脚本中进行了更改之后,我必须重新启动Python,再次进行所有导入,创建对象并输入参数。

$ python
>>> from foo import bar
>>> from package.file import Class
>>> c = Class
>>> c.name = "John"
>>> c.age = 33
>>> c.function()
>>> from datetime import timedelta, datetime
>>> now = datetime.now().year()
>>> next_year = now + timedelta(year=1)
>>> etc...

Can someone tell me if there is an easier way then doing all the work over and over again every time I make a change in the Python code? 有人可以告诉我是否有一种更简便的方法,然后每当我对Python代码进行更改时,一遍又一遍地进行所有工作?

Use IPython with a notebook instead. 而是将IPython与笔记本一起使用。 Much better for interactive computing. 交互式计算要好得多。

You could consider turning your testing into an actual python script. 您可以考虑将测试转换为实际的python脚本。 which can be run like this, and then checking the output 可以像这样运行,然后检查输出

$ python my_tests.py

However, a much better way would be to write some unit tests which you can run in a similar way. 但是,更好的方法是编写一些可以以类似方式运行的单元测试。 https://docs.python.org/2/library/unittest.html . https://docs.python.org/2/library/unittest.html The unittest framework will run all the tests you've defined and gather the results into a report. unittest框架将运行您定义的所有测试,并将结果收集到报告中。

If you need some steps to be done interactively, then you can achieve that by writing your setup into a script, and then executing the script before doing your interactive tests. 如果需要以交互方式完成一些步骤,则可以通过将设置写入脚本中,然后在执行交互式测试之前执行脚本来实现此目的。 See this other SO question: Is there a possibility to execute a Python script while being in interactive mode 看到另一个SO问题: 在交互模式下是否有可能执行Python脚本

Going for IPython as well. 同样适用于IPython。 You can write a script which will enter an interactive shell at any point in any frame, for example: 您可以编写一个脚本,该脚本将在任何框架中的任何点进入交互式外壳,例如:

import IPython
from foo import bar
from package.file import Class
c = Class
c.name = "John"
c.age = 33
c.function()
from datetime import timedelta, datetime
now = datetime.now().year()
next_year = now + timedelta(year=1)
IPython.embed()

Then simply run your script with python, and you'll get an interactive shell at the end. 然后,只需使用python运行脚本,最后您将获得一个交互式shell。

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

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