3

I really love python because I love interactive development. There's one area where python appears to fall short, however, and that's in the area of automatically reloading changed files. Basically, what I want to have happen is to be able to modify a python file on-disk and then have my running python instance automatically reload the changed module to allow me to immediately access my changes in the REPL so I can test them out. Basically, I want some sort of watch command.

I happen to use the bpython shell because I think it's the best one available, but this feature is so important to me that I'd be willing to switch to any other python shell that does it right. Is it possible?

Derek Thurn
  • 14,953
  • 9
  • 42
  • 64
  • something like this: http://stackoverflow.com/questions/4514095/dynamically-loading-python-source-code ? – Mariy Jan 18 '11 at 22:24

2 Answers2

0

Something like tail -f in python + reload().

I really think they should make the tags OS and Python version mandatory though.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • 1
    `reload` has more corner cases than actual functionality; avoid it whenever possible. – habnabit Jan 18 '11 at 22:17
  • And that makes my whole answer not-useful? Sometimes module reloads can't be avoided. – orlp Jan 18 '11 at 22:23
  • @nightcracker, can you give an example? Basically every time I've seen someone suggest that, the problem would've been better solved either with unit testing or restarting the python interpreter. – habnabit Jan 18 '11 at 22:25
  • Development on servers where the server can't be interrupted. – orlp Jan 18 '11 at 22:27
  • @nightcracker, okay, and how is this not solvable by restarting the python interpreter? Have a pool of running interpreters, and reverse-proxy any incoming requests to them. If code has changed, restart them. – habnabit Jan 18 '11 at 22:29
  • I don't really understand the suggestion here. I'm usually in a UNIX environment, so using the native tail command isn't a problem. How do get the python interpreter to reload a module by using tail? – Derek Thurn Jan 19 '11 at 02:41
0

If you're trying to "test out" your code, perhaps you should be looking into doing automated unit tests instead of testing your code repeatedly and manually. It'll allow you to test more code quicker and waste less of your precious, precious development time.

Personally, I use unittest with py.test as the runner.

habnabit
  • 9,906
  • 3
  • 32
  • 26
  • If you are doing a web development this is not the case. For example automatic server reloads. – Mariy Jan 18 '11 at 22:37
  • @cldy, any half-decent web development framework I've seen comes with ways to automate integration tests, and doesn't preclude you from being able to write unit tests. Why do you need a server at all? – habnabit Jan 18 '11 at 22:39
  • I mean you write something and want to see it on your browser. It is much easier to be automatically loaded instead of restarting it. – Mariy Jan 18 '11 at 22:42
  • @cldy, as I mentioned in the comment thread on nightcracker's answer, this is easily solvable by having a pool of worker python interpreters to which requests are reverse-proxied. When code changes, kill the workers and spawn new ones. Much simpler, and no corner cases as there are with `reload`. – habnabit Jan 18 '11 at 22:56
  • In fact, I prefer to develop my unit tests interactively and then copy them into a file for posterity. It's a method that works for me. – Derek Thurn Jan 19 '11 at 02:35
  • Sorry but I had to downvote that answer. Playing around with your code in the REPL and writing unit tests are two different things. Unit tests are a good thing, but they don't allow interactive development like the REPL does. – S4M Oct 30 '15 at 14:35
  • @S4M, no? How not? Often I've written unit tests that just do `assert the_ultimate_result == ()` just to see what the result is before finishing with the implementation and tests. I can change the implementation to see how the result differs, and when I'm satisfied, fill in the test with the answer I actually want. That's as interactive as using the REPL, and makes you think about how to write the tests. – habnabit Mar 14 '16 at 22:08