0

It is very nice and easy to run Python from the command line. 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
>>> 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?

Johan Vergeer
  • 5,208
  • 10
  • 48
  • 105
  • It would help if you were clearer about what you're trying to do after changing the script. Are you testing functionality? – Harry Harrison Nov 15 '15 at 17:35
  • Possible duplicate of [How do I watch a file for changes using Python?](http://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes-using-python) – Papouche Guinslyzinho Nov 15 '15 at 17:40

3 Answers3

1

Use IPython with a notebook instead. Much better for interactive computing.

Adam Acosta
  • 603
  • 3
  • 6
1

You could consider turning your testing into an actual python script. 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. The unittest framework will run all the tests you've defined and gather the results into a report.

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

Community
  • 1
  • 1
Harry Harrison
  • 591
  • 4
  • 12
0

Going for IPython as well. 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.

micromoses
  • 6,747
  • 2
  • 20
  • 29