1

I'm working on writing my first Python class. Being the java programmer that I am, I have something like this:

#class1.py
class class1:
 def __init__(self):
 #Do stuff here

And in my current script:

import class1
object = class1()

I'm getting a Name Error: name 'class1' is not defined

I've also tried this, with no luck:

import class1
object = class1.class1()

The error I get here is AttributeError: 'module' object has no attribute 'class1'

What am I doing wrong?

asked Jan 21, 2013 at 17:13
8
  • What's the actual name of the file / class? The second method should work. Commented Jan 21, 2013 at 17:15
  • tle.py/ tle. Not sure why it makes a difference... Commented Jan 21, 2013 at 17:16
  • @ThomasOrozco: No it shouldn't. The module name and class name are not required to be the same, nor are they the same thing. Commented Jan 21, 2013 at 17:17
  • 1
    @ThomasOrozco: Ah, I missed the second attempt. Commented Jan 21, 2013 at 17:19
  • 1
    This is an additional note. Since you are coming from Java to Python you may want to learn about good programming practices in Python. This link will tell you just about everything you need to know for good Python practices. Commented Jan 21, 2013 at 17:24

2 Answers 2

4

Python import is by module and then by the contents of the module, so for your class1.py it becomes:

from class1 import class1

Python module docs

sean
3,98523 silver badges28 bronze badges
answered Jan 21, 2013 at 17:16
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, that simple... Thanks!
Well, this works, but it doesn't explain why class1.class1() can't be used as a constructor when the class1 module has been successfully imported.
@JohnY That would have worked but it says in the comments under his question that his file name is tle.py. So they were importing the wrong module. Strange why there was no ImportError in that case though.
@StuartRobertson: No, the file name tle.py in the comments was just to confirm that the OP didn't just happen to pick a module name conflicting with something else. For purposes of the question, the module is the file class1.py, containing the class definition class1, as given in the text of the question. And ordinarily, you should be able to create an instance of class1 by first importing the class1 module, and then calling class1.class1().
-1

In Python you import the modules. For class1.py file you can use:

from class1 import class1

Or if you have more than one....

from class1 import *
answered Jan 21, 2013 at 17:20

Comments

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.