Python import : AttributeError: 'module' object has no attribute 'test' -
i think that's stupid problem, can't figure out why following
attributeerror: 'module' object has no attribute 'test' when running test3.py.
here project tree :
. ├── __init__.py ├── test3.py └── testdir ├── __init__.py └── test.py my test3.py :
#!/usr/bin/python import testdir if __name__ == "__main__": print(testdir.test.var) my test.py :
#!/usr/bin/python import os var=os.path.abspath(__file__) i tried import var way :
from testdir.test import var edit: 1 works -thanks @user2357112- still know how import whole test.py file without from ... import ... if possible. :)
and tried import ..testdir relative import got syntaxerror.
and if try import testdir.test nameerror: name'test' not defined.
how import file? i'm bit confused.
edit bis :
i apologize, when tried import testdir.test, modified print(testdir.test.var) print(test.var).
that problem, bad.
with :
#!/usr/bin/python import testdir.test if __name__ == "__main__": print(testdir.test.var) it works perfectly, though importing testdir.test made test exist alone (and not testdir.test) in scope.
sorry inconvenience. :s
try adding from .test import var testdir/init.py. import testdir print testdir.var in test3.py might work. agree more of workaround solution.
Comments
Post a Comment