1

I'm very new to Python and I have a code like this:

class Configuration:
 @staticmethod
 def test():
 return "Hello World"

When I call the method test from other python code like this:

import test
test.Configuration.test()

I get an error like this:

Traceback (most recent call last):
 File "example.py", line 3, in <module>
 test.Configuration.test()
AttributeError: 'module' object has no attribute 'test'

where I'm making the mistake?

Edit:

My directory structure:

root
--example.py
--test
----__init.py__
----Configuration.py
asked Apr 22, 2013 at 10:11
15
  • Do you have a directory test with a file Configuration.py in it somewhere? Or did you have a file Configuration.py that you imported in test? Commented Apr 22, 2013 at 10:11
  • What does import test; print test.Configuration.__file__ print? Commented Apr 22, 2013 at 10:12
  • If you mean another python file by saying another python code, you should create an empty __init__.py in same directory with your test function located. Commented Apr 22, 2013 at 10:16
  • 1
    @ali: The OP is not trying to create a package here. That is not the problem. Commented Apr 22, 2013 at 10:16
  • @MartijnPieters: Its printing the Configuration location.. like /dir/Configuration.pyc. Commented Apr 22, 2013 at 10:17

2 Answers 2

2

Python module names and the classes they contain are separate. You need use the full path:

import test
print test.Configuration.Configuration.test()

Your test package has a module named Configuration, and inside that module is your Configuration class.

Note that Python, unlike Java, lets you define methods outside classes too, no need to make this a static method. Nor do you need to use a separate file per class.

answered Apr 22, 2013 at 10:27
Sign up to request clarification or add additional context in comments.

Comments

0

Try to rename your module to something other than 'test', since this is the name of a standard library module (http://docs.python.org/2/library/test.html) and probably you're importing that module instead of your own. Another option is to add the directory containing your test module into the PYTHONPATH environment variable, so that python may find it instead of the standard library module (but this is not advised as it shadows the standard module and you won't be able to import it later).

To check which file you're importing from, do:

import test
print test
answered Apr 22, 2013 at 10:19

1 Comment

There is no Configuration file in the python test package. There is no Configuration.py file anywhere in the Python library.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.