question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reloading imported modules

See original GitHub issue

I have a feature request, or at least a discussion on one.

Use case

NB. This is very simplified, I have one project with 8 different local module imports, and I intend to use more in the next.

driving_dims.py:

box = (100, 20, 30)

main.py:

import cadquery as cq
import driving_dims

a = cq.Workplane("front").box(*driving_dims.box)

show_object(a)
  1. start cq-editor, load main.py, render
  2. change box in driving_dims.py edit: just realised it’s relevant that I use vim and edit all my files externally from cq-editor
  3. render again, new dimensions are not loaded

User side solution

I naively restarted cq-editor a lot until I discovered I could change my import from

import driving_dims

to

import importlib
import driving_dims
importlib.reload(driving_dims)

to have the module reloaded every time the script is executed.

I kind of like this solution because you can be explicit about what needs to be reloaded every time.

Feature suggestion

I would be happy with just documenting the importlib.reload method, but the devs seem to be going for a quite user-friendly feel, so perhaps they want to implement automatic reloading of any module in the working script directory?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lorenzncodecommented, Aug 21, 2021

This was implemented in #272.

  1. start cq-editor, load main.py, render
  2. change box in driving_dims.py
  3. render again, new dimensions are loaded

With Preferences “Autoreload: watch imported modules” selected:

  1. start cq-editor, load main.py, render
  2. change box in driving_dims.py, render updates automatically
1reaction
adam-urbanczykcommented, Feb 5, 2020

It should be possible, e.g. like so:

  • parse the code with AST and find import statements
  • reload the modules is they are already loaded This will have low prio for me at the moment though.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reloading modules in Python - GeeksforGeeks
The reload() is a previously imported module. If you've altered the module source file using an outside editor and want to test the...
Read more >
Reloading a Module - Real Python
In this video, I'll show you about reloading a module. Up to now, you've been simply importing a module once. Even if you...
Read more >
Reloading modules in Python? - Tutorialspoint
The reload() is used to reload a previously imported module or loaded module. This comes handy in a situation where you repeatedly run...
Read more >
How do I unload (reload) a Python module? - Stack Overflow
You can reload a module when it has already been imported by using importlib.reload() : from importlib import reload # Python 3.4+ import...
Read more >
Reloading All Loaded Modules - Python Cookbook [Book]
You can use the reload function, but this is difficult if you perform changes in a module that isn't directly imported by your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found