4

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

4
  • 2
    Is vAR capitalized like that in your actual file? Commented Jun 14, 2015 at 6:49
  • My bad, you're right. Now from testdir.test import VAR works, thank you. But how could I import the whole file without using a from ... import ...? Commented Jun 14, 2015 at 7:03
  • import testdir.test. Importing a package doesn't automatically load its submodules. Commented Jun 14, 2015 at 7:05
  • I tried this but I got a NameError. Commented Jun 14, 2015 at 7:07

4 Answers 4

1

Try adding from .test import VAR to testdir/init.py. Then import testdir print testdir.VAR in test3.py might work. I agree it is more of a workaround than solution.

Sign up to request clarification or add additional context in comments.

2 Comments

This workaround works, indeed, thank you. :) I note this way to do, I didn't know it (I don't understand everything about imports). But you're right : that's a workaround, isn't it dirty to put such imports in the _init_.py? Anyway I finally found the problem (and I made a mistake in my question, so nobody would have be able to give me the answer to my problem, my bad :S).
No it is not 'dirty' per se to put imports in init file, infact the name suggests it is for initialisation of a package. It provides a sort of encapsulation, in which users of the package don't need to know about the internal structure and modules of the package. With that said, importing everything in init do clutters the namespace. So use it wisely.
1

For adding the whole file with out using import statement, you can use execfile

1 Comment

I didn't know this one, thank you. But if I use it, it may, in some cases, erase some objects. I'll use it for something else though.
1

I know this one is old, but I had the same error relating to argparse. I had an instance where you'd run the script and pass test if you want to run tests (might be a weird way to do it, but I am new to this!). It would pass test to unittest.main() as well. I fixed it with this:

unittest.main(argv=['_'])

Below is what I had when it was working:

from tests import *

parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest='command', title='Commands')
subcommand_parser = subparser.add_parser('test')
args.parse_args()

if args.command == 'test':
  unittest.main(argv=['_'])

This also helped me: https://stackoverflow.com/a/52369043/13752446

1 Comment

Hello and welcome on Stack Overflow. This rather sounds as an AttributeError and is kinda off-topic. I'd suggest rather making a separate issue ( assuming the question hasn't been asked yet , which is quite possible) describing the problem, the detailed error, the code giving the error, and then answering your own post with the solution you found.
0

I finally succeed to make it work this way :

#!/usr/bin/python                                                          

import testdir.test

if __name__ == "__main__":
    print(testdir.test.VAR)

I erroneously modified print(testdir.test.VAR) to print(test.VAR) when I tried with import testdir.test. So I had this NameError.

My bad.

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.