Search code examples
pythonmoduleattributeerror

Python custom module attribute error _name_


I observe a very strange behavior with python: I get an attribute error for any custom module for any attribute or function, although dir() tells me it exists.

What I do:

I have a module called testmodule.py:

def printing(text):
   print("something and "+text)

In my main I do:


import testmodule as tm

print(dir(tm))
print(tm._name_)

What I get:

The output of dir() looks as expected: ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'printing']

But then I get: AttributeError: module 'testsite_function' has no attribute '_file_'. This happens no matter which attribute or function I call.

More: I do not know whether this is useful information: I use an python 3.8.10 interpreter. I am working in an virtual enviroment. It is activated and has caused no problems so far. Both, main and testmodule.py are in the same folder. I am working with vs code in remote via ssh. This caused no problems so far. A few days ago there were no such problems. I reinstalled vscode in the meantime, with the same version as before. The same problem occures when executing main on the terminal directly. Other modules like numpy work.


Solution

  • You need to use double underscores:

    print(tm.__name__)