Live data from Hacker News

Jurigged – Hot code reloading for Python

github.com

11–20 of 33 posts

Re: Jurigged – Hot code reloading for Python

#11
post #4

Wait what? I thought Python was an interpreted language. Did I miss something?

Imported modules are compiled to bytecode and frozen on import, so changing the code in-place won't change what actually get run when the code is called unless you clear the bytecode cache and re-import. importlib provide a reload method for this purpose: https://docs.python.org/3.8/library/importlib.html#importlib...

Interesting. I've done it with no issues on both 27 and 36. Just edit the code directly in site-packages.

Re: Jurigged – Hot code reloading for Python

#13
post #4

Wait what? I thought Python was an interpreted language. Did I miss something?

Python doesn't re-read a function's source code from the filesystem every time it calls it, and that would be a bad idea in general since you'd inadvertently crash your running programs during development.

This watches for changes on the filesystem and injects them into the running program as well as it can, basically keeping the current program state but with new behaviour.

This is kind of a footgun and there are many changes you can make that will crash or ruin the process, but if you're aware of the limitations it can be pretty useful.

Re: Jurigged – Hot code reloading for Python

#15
If you use iPython you can enable auto reloading for all your sessions. It works really well - I've been using it for years. You need to know the limitations, but on the whole it give huge performance gains when I'm coding.

To enable it add the following to your ipython_config.py:

    c = get_config()
    c.InteractiveShellApp.exec_lines = ['%autoreload 2']
    c.InteractiveShellApp.extensions = ['autoreload']

Re: Jurigged – Hot code reloading for Python

#16
post #15

If you use iPython you can enable auto reloading for all your sessions. It works really well - I've been using it for years. You need to know the limitations, but on the whole it give huge performance gains when I'm coding. To enable it add the following to your ipython_config.py: c = get_config() c.InteractiveShellApp.exec_lines = ['%autoreload 2'] c.InteractiveShellApp.extensions = ['autoreload']

You can also run `jurigged -vm IPython`, for what it's worth (or put `import jurigged; jurigged.watch()` in the config). There are a few edge cases where jurigged will work and not IPython's autoreload, and vice versa. Notably, jurigged updates decorated functions a bit more aggressively and only runs the code diff so it may keep some state that IPython won't.

Re: Jurigged – Hot code reloading for Python

#17

This looks impressive, and equally a recipe for utter confusion. I've always thought that it's a brilliant feature of Python that you can edit code while it's running and not affect the process; unlike, say, bash scripts.

In a proper managed environment (whatever that may mean...), this could be useful as a means for continuous integration. Maybe not worth all the hassle, but maybe there's a usecase where this would be a beloved silver bullet.

We use something similar in some of the web crawlers in my company, we dynamically load the module that selects the data from a web page so that if something (website and/or requirement) changes we can just swap the module without downtime.

Re: Jurigged – Hot code reloading for Python

#18
post #12

Looks more usable than the reloading library which does a similar thing, going to be interesting to use them together. I found Reloading when it was discussed in 2019: https://news.ycombinator.com/item?id=21268324

Hey author of reloading here - cool to see this mentioned. Would love to know how reloading could be more usable for you.

Since the post on HN in 2019, reloading can also be used as a decorator to load functions from source before execution. For some use cases that may work better than the reloading loop.

Post reply on HN