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?

2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.