Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload all imported modules?

I have several modules, I would like to reload without having to restart Sublime Text, while I am developing a Sublime Text package.

I am running Sublime Text build 3142 which comes with python3.3 running continuously its packages/plugins. However while developing a plugin, I import a third part module I added to path as:

import os
import sys

def assert_path(module):
    """
        Import a module from a relative path
        https://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path
    """
    if module not in sys.path:
        sys.path.insert( 0, module )

current_directory = os.path.dirname( os.path.realpath( __file__ ) )
assert_path( os.path.join( current_directory, 'six' ) ) # https://github.com/benjaminp/six

import six

But when I edit the source code of the module six I need to close and open Sublime Text again, otherwise Sublime Text does not gets the changes to the six python module.


Some code I have tried so far:

print( sys.modules )
import git_wrapper
imp.reload( find_forks )
imp.reload( git_wrapper )
imp.reload( sys )
  1. Proper way to reload a python module from the console
  2. Reloading module giving NameError: name 'reload' is not defined
like image 574
user Avatar asked Jul 30 '17 23:07

user


1 Answers

To list all imported modules, you can use sys.modules.values().

import sys
sys.modules.values()

sys.modules is a dictionary that maps the string names of modules to their references.

To reload modules, you can loop over the returned list from above and call importlib.reload on each one:

import importlib
for module in sys.modules.values():
    importlib.reload(module)
like image 109
cs95 Avatar answered Sep 26 '22 14:09

cs95