I think that's stupid problem, but I can't figure out why I get the following
AttributeError: 'module' object has no attribute 'test'
when running my test3.py.
Here is my 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 also tried to import my VAR this way :
from testdir.test import VAR
EDIT: Now this one works -thanks to @user2357112- but I would still like to know how to import the whole test.py file without a from ... import ...
if it is possible. :)
And I tried a import ..testdir
to do a relative import but I got a SyntaxError
.
And if I try import testdir.test
I get a NameError: name'test' is not defined
.
How could I import this file? I'm a bit confused.
EDIT bis :
I apologize, when I tried import testdir.test
, I also modified print(testdir.test.VAR)
to print(test.VAR)
.
That was the problem, my bad.
with :
#!/usr/bin/python
import testdir.test
if __name__ == "__main__":
print(testdir.test.VAR)
It works perfectly, I though that importing testdir.test
made test
exist alone (and not testdir.test
) in the scope.
Sorry for the inconvenience. :S
I finally succeed to make it work this way :
I erroneously modified
print(testdir.test.VAR)
toprint(test.VAR)
when I tried withimport testdir.test
. So I had this NameError.My bad.