0

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.

1 Answer 1

1

You need to use double underscores:

print(tm.__name__)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.