Python: "de-import", "re-import", "reset import"? -


i debug (in pycharm) script. stop @ breakpoint, go debug console window , there, invoke import line, this:

import my_util1 my_utils 

then call my_util1. far, ok. change in "my_util1". call (updated) my_util1 cannot: system (python? pycharm?) "sees" previous version of my_util1.

is there possibility "reset" (refresh) imported earlier, or "re-import" it, other exiting pycharm , restarting project?

it not dynamically changing actual code being debugged. task looking simpler - suffice undo 'import' operation, or reset/clear/refresh 'imports' @ once. additionally, action done within debugger window, not in code window.

sys.modules can manipulated change python's ideas of what's imported. quote python docs:

this dictionary maps module names modules have been loaded. can manipulated force reloading of modules , other tricks. however, replacing dictionary not work expected , deleting essential items dictionary may cause python fail.

sample usage:

import sys import my_util1 my_utils  # sys.modules['my_utils'] exists , my_util1 local name.  del sys.modules['my_utils']  # my_util1 still exists local name, python has "forgotten" import.  import my_util1 my_utils # re-imports my_utils , binds new my_util1. 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -