Imp.reload - the NoneType object does not have the attribute 'name'

The following code:

def _IMPORT_(path) path = abspath(path) namespace = path[len(getcwd())+1:].replace('/', '_').strip('\\/;,. ') print(path) print(namespace) loader = importlib.machinery.SourceFileLoader(namespace, path+'.py') handle = loader.load_module(namespace) print(handle) importlib.reload(handle) return handle 

It produces:

 /home/torxed/git/test/unitest/unix unitest_unix <module 'unitest_unix' from '/home/torxed/git/test/unitest/unix.py'> Traceback (most recent call last): File "network.py", line 17, in <module> handle = sock() File "network.py", line 9, in __init__ sock = _IMPORT_('./unix') File "/home/torxed/git/test/unitest/helpers.py", line 13, in _IMPORT_ imp.reload(handle) File "/usr/lib/python3.4/imp.py", line 315, in reload return importlib.reload(module) File "/usr/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' 

This works fine in Python 3.3.5, where such an operation was introduced (there used to be the same imported mechanics). However, on Python 3.4.2, this does not seem to work.

What has changed since 3.3.5? No trace can be found or the change is somewhere in the middle of releases. Last year, a patch was fixed for this exact behavior when environment variables were not passed, but it seems to work here.

+11
source share
2 answers

I get the same error when I try to reload a file after switching folders.

For instance:


Create a simple module:

 In [10]: %%file temp.py ...: message = "Hello World!" ...: Writing temp.py 

Download the module and print the message:

 In [14]: import temp ...: print(temp.message) Hello World! 

Change the message:

 In [17]: temp.message = 'Hello brave new world!' ...: print(temp.message) Hello brave new world! 

Reboot the module to return the original message:

 In [18]: import imp ...: imp.reload(temp) ...: print(temp.message) Hello World! 

Everything is fine so far ...


Now change the paths:

 In [20]: cd .. 

Try reloading the module:

 In [24]: imp.reload(temp) Traceback (most recent call last): File "<ipython-input-24-7fa95de0f250>", line 1, in <module> imp.reload(temp) File "/home/user/anaconda3/lib/python3.4/imp.py", line 315, in reload return importlib.reload(module) File "/home/user/anaconda3/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' 

In my case, the solution was to get back to the path in which the import was made initially.

+8
source

Indeed, importlib.reload only works if you are in the correct directory. To get around this, I wrote a helper function that I can call:

 def reload( module ): import os import os.path import importlib cwd = os.getcwd() os.chdir( os.path.dirname(os.path.abspath(module.__file__)) ) importlib.reload( module ) os.chdir( cwd ) 
0
source

Source: https://habr.com/ru/post/979050/


All Articles