Search code examples
pythonimportmodulereload

Can't reload module that is in another folder but also in sys.path


I'm having some troubles reloading a module that is in another directory but is in sys.path.

>>> from module_from_another_dir import *
>>> from importlib import reload
>>> reload(module_from_another_dir)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'module_from_another_dir' is not defined

So as you can see I can import it, but I can't reload it later.


Solution

  • When you import module like this

    from module_from_another_dir import *
    

    then Python knows only new methods and "forgets" about module name. However, you can import your module as

    import module_from_another_dir
    

    and you will be able to reload it easily.