reload a module imported with importlib.machinery.SourceFileLoader

Tal Einat taleinat at gmail.com
Mon Mar 2 03:08:14 EST 2015


Hi everyone,

I'm trying to import a module with tests, run the tests, then patch
some things with mock.patch, reload the tests module and re-run the
tests. This is part of an interactive course in which the tests module
is written by the user. Therefore I'm using
importlib.machinery.SourceFileLoader to import the tests module, like
so:

importlib.machinery.SourceFileLoader("tmp", path).load_module("tmp")

This works just fine. However, when trying to reload the module,
importlib.reload(module) is raising the following exception:

Traceback (most recent call last):
  File ".../tests.py", line 28, in <module>
    reload_module(module)
  File ".../custom_test_helpers.py", line 18, in reload_module
    importlib.reload(module)
  File ".../lib/python3.4/importlib/__init__.py", line 149, in reload
    methods.exec(module)
  File "<frozen importlib._bootstrap>", line 1134, in exec
AttributeError: 'NoneType' object has no attribute 'name'

Am I going about this the wrong way? Is this a bug in importlib? If it
is a bug, and suggestions for a workaround?

I'm running Python 3.4.2, installed via pyenv on OSX 10.8.

- Tal Einat



More information about the Python-list mailing list