Reputation: 16081
I have the module Test.py with class test inside the module. Here is the code:
class test:
SIZE = 100;
tot = 0;
def __init__(self, int1, int2):
tot = int1 + int2;
def getTot(self):
return tot;
def printIntegers(self):
for i in range(0, 10):
print(i);
Now, at the interpreter I try:
>>> import Test
>>> t = test(1, 2);
I get the following error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
t = test(1, 2);
NameError: name 'test' is not defined
Where did I go wrong?
Upvotes: 0
Views: 78
Reputation: 21368
Your question has already been answered by @larsmans and @Votatility, but I'm chiming in because nobody mentioned that you're violating Python standards with your naming convention.
Modules should be all lower case, delimited by underscores (optional), while classes should be camel-cased. So, what you should have is:
test.py:
class Test(object):
pass
other.py
from test import Test
# or
import test
inst = test.Test()
Upvotes: 1
Reputation: 32300
You have to access the class like so:
Test.test
If you want to access the class like you tried to before, you have two options:
from Test import *
This imports everything from the module. However, it isn't recommended as there is a possibility that something from the module can overwrite a builtin without realising it.
You can also do:
from Test import test
This is much safer, because you know which names you are overwriting, assuming you are actually overwriting anything.
Upvotes: 6
Reputation: 363547
When you do import Test
, you can access the class as Test.test
. If you want to access it as test
, do from Test import test
.
Upvotes: 0