Can't import python classes











up vote
0
down vote

favorite












I have a simple error with the import
But how to solve it?;))
I'm noob and it is testing task



One of the conditions is to separate the parent class from the child!
That is, everything good with their location [classes_1.py, classes_2.py]



Main:



n10_12.py

from classes_1 import User
from classes_2 import Privileges, Admin

admin_33 = Admin('mike', 'logsdale', 'africa', 'customer')

admin_33.privilege.show_privileges()
admin_33.descibe_user()


Classes two file:



1)



classes_1.py

class User():

def __init__(self, first_name, last_name, location='', field=''):
self.first_name = first_name
self.last_name = last_name
self.location = location
self.field = field
self.full_name = self.first_name.title() + ' ' + self.last_name.title()
self.login_attemts = 0

def descibe_user(self):

print('nFull name: ' + self.full_name +
'nLocation: ' + self.location.title() +
'nField: ' + self.field.title() + 'n'
)

def greet_user(self):
print('Hello, ' + self.full_name)

def read_login_attemts(self):
print('In this time login attempts are ' + str(self.login_attemts))

def increment_login_attempts(self):
self.login_attemts += 1

def reset_login_attempts(self):
self.login_attemts = 0


2)



classes_2.py

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())




class Admin(User):

def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()


Traceback:



python n10_12.py



Traceback (most recent call last):



File "n10_12.py", line 2, in
from classes_2 import Privileges, Admin



File ..classes_2.py", line 28, in
class Admin(User):



NameError: name 'User' is not defined










share|improve this question
























  • The error is clear, User is undefined in your second .py file.
    – juanpa.arrivillaga
    Nov 19 at 17:30















up vote
0
down vote

favorite












I have a simple error with the import
But how to solve it?;))
I'm noob and it is testing task



One of the conditions is to separate the parent class from the child!
That is, everything good with their location [classes_1.py, classes_2.py]



Main:



n10_12.py

from classes_1 import User
from classes_2 import Privileges, Admin

admin_33 = Admin('mike', 'logsdale', 'africa', 'customer')

admin_33.privilege.show_privileges()
admin_33.descibe_user()


Classes two file:



1)



classes_1.py

class User():

def __init__(self, first_name, last_name, location='', field=''):
self.first_name = first_name
self.last_name = last_name
self.location = location
self.field = field
self.full_name = self.first_name.title() + ' ' + self.last_name.title()
self.login_attemts = 0

def descibe_user(self):

print('nFull name: ' + self.full_name +
'nLocation: ' + self.location.title() +
'nField: ' + self.field.title() + 'n'
)

def greet_user(self):
print('Hello, ' + self.full_name)

def read_login_attemts(self):
print('In this time login attempts are ' + str(self.login_attemts))

def increment_login_attempts(self):
self.login_attemts += 1

def reset_login_attempts(self):
self.login_attemts = 0


2)



classes_2.py

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())




class Admin(User):

def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()


Traceback:



python n10_12.py



Traceback (most recent call last):



File "n10_12.py", line 2, in
from classes_2 import Privileges, Admin



File ..classes_2.py", line 28, in
class Admin(User):



NameError: name 'User' is not defined










share|improve this question
























  • The error is clear, User is undefined in your second .py file.
    – juanpa.arrivillaga
    Nov 19 at 17:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a simple error with the import
But how to solve it?;))
I'm noob and it is testing task



One of the conditions is to separate the parent class from the child!
That is, everything good with their location [classes_1.py, classes_2.py]



Main:



n10_12.py

from classes_1 import User
from classes_2 import Privileges, Admin

admin_33 = Admin('mike', 'logsdale', 'africa', 'customer')

admin_33.privilege.show_privileges()
admin_33.descibe_user()


Classes two file:



1)



classes_1.py

class User():

def __init__(self, first_name, last_name, location='', field=''):
self.first_name = first_name
self.last_name = last_name
self.location = location
self.field = field
self.full_name = self.first_name.title() + ' ' + self.last_name.title()
self.login_attemts = 0

def descibe_user(self):

print('nFull name: ' + self.full_name +
'nLocation: ' + self.location.title() +
'nField: ' + self.field.title() + 'n'
)

def greet_user(self):
print('Hello, ' + self.full_name)

def read_login_attemts(self):
print('In this time login attempts are ' + str(self.login_attemts))

def increment_login_attempts(self):
self.login_attemts += 1

def reset_login_attempts(self):
self.login_attemts = 0


2)



classes_2.py

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())




class Admin(User):

def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()


Traceback:



python n10_12.py



Traceback (most recent call last):



File "n10_12.py", line 2, in
from classes_2 import Privileges, Admin



File ..classes_2.py", line 28, in
class Admin(User):



NameError: name 'User' is not defined










share|improve this question















I have a simple error with the import
But how to solve it?;))
I'm noob and it is testing task



One of the conditions is to separate the parent class from the child!
That is, everything good with their location [classes_1.py, classes_2.py]



Main:



n10_12.py

from classes_1 import User
from classes_2 import Privileges, Admin

admin_33 = Admin('mike', 'logsdale', 'africa', 'customer')

admin_33.privilege.show_privileges()
admin_33.descibe_user()


Classes two file:



1)



classes_1.py

class User():

def __init__(self, first_name, last_name, location='', field=''):
self.first_name = first_name
self.last_name = last_name
self.location = location
self.field = field
self.full_name = self.first_name.title() + ' ' + self.last_name.title()
self.login_attemts = 0

def descibe_user(self):

print('nFull name: ' + self.full_name +
'nLocation: ' + self.location.title() +
'nField: ' + self.field.title() + 'n'
)

def greet_user(self):
print('Hello, ' + self.full_name)

def read_login_attemts(self):
print('In this time login attempts are ' + str(self.login_attemts))

def increment_login_attempts(self):
self.login_attemts += 1

def reset_login_attempts(self):
self.login_attemts = 0


2)



classes_2.py

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())




class Admin(User):

def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()


Traceback:



python n10_12.py



Traceback (most recent call last):



File "n10_12.py", line 2, in
from classes_2 import Privileges, Admin



File ..classes_2.py", line 28, in
class Admin(User):



NameError: name 'User' is not defined







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 17:22

























asked Nov 19 at 17:06









Izzumi

32




32












  • The error is clear, User is undefined in your second .py file.
    – juanpa.arrivillaga
    Nov 19 at 17:30


















  • The error is clear, User is undefined in your second .py file.
    – juanpa.arrivillaga
    Nov 19 at 17:30
















The error is clear, User is undefined in your second .py file.
– juanpa.arrivillaga
Nov 19 at 17:30




The error is clear, User is undefined in your second .py file.
– juanpa.arrivillaga
Nov 19 at 17:30












2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










you need to import User in classes_2.py :



from classes_1 import User

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())

class Admin(User):
def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()





share|improve this answer





















  • Sorry, I forgot to specify conditions.. (One of the conditions is to separate the parent class from the child! That is, everything good with their location [classes_1.py, classes_2.py])
    – Izzumi
    Nov 19 at 17:24












  • @Izzumi ok, you need to import it then. You should know, btw, that this isn't a common pattern in Python, that is, all classes in separate files.
    – juanpa.arrivillaga
    Nov 19 at 17:31












  • @Izzumi Not sure to understand. Aren't they already separated ? (Admin is in classes_2, User in classes_1 ?). Or do you mean that you want to specify later which class Admin inherits ? (in n10_12.py)
    – Corentin Limier
    Nov 19 at 18:02










  • @juanpa.arrivillaga Thanks I got it ;)
    – Izzumi
    Nov 20 at 10:11




















up vote
0
down vote













I think your second classes file should be importing the first classes file;otherwise, how does it know what User is?






share|improve this answer





















  • such a condition was given in the assignment, so it should be
    – Izzumi
    Nov 19 at 17:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379518%2fcant-import-python-classes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










you need to import User in classes_2.py :



from classes_1 import User

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())

class Admin(User):
def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()





share|improve this answer





















  • Sorry, I forgot to specify conditions.. (One of the conditions is to separate the parent class from the child! That is, everything good with their location [classes_1.py, classes_2.py])
    – Izzumi
    Nov 19 at 17:24












  • @Izzumi ok, you need to import it then. You should know, btw, that this isn't a common pattern in Python, that is, all classes in separate files.
    – juanpa.arrivillaga
    Nov 19 at 17:31












  • @Izzumi Not sure to understand. Aren't they already separated ? (Admin is in classes_2, User in classes_1 ?). Or do you mean that you want to specify later which class Admin inherits ? (in n10_12.py)
    – Corentin Limier
    Nov 19 at 18:02










  • @juanpa.arrivillaga Thanks I got it ;)
    – Izzumi
    Nov 20 at 10:11

















up vote
4
down vote



accepted










you need to import User in classes_2.py :



from classes_1 import User

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())

class Admin(User):
def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()





share|improve this answer





















  • Sorry, I forgot to specify conditions.. (One of the conditions is to separate the parent class from the child! That is, everything good with their location [classes_1.py, classes_2.py])
    – Izzumi
    Nov 19 at 17:24












  • @Izzumi ok, you need to import it then. You should know, btw, that this isn't a common pattern in Python, that is, all classes in separate files.
    – juanpa.arrivillaga
    Nov 19 at 17:31












  • @Izzumi Not sure to understand. Aren't they already separated ? (Admin is in classes_2, User in classes_1 ?). Or do you mean that you want to specify later which class Admin inherits ? (in n10_12.py)
    – Corentin Limier
    Nov 19 at 18:02










  • @juanpa.arrivillaga Thanks I got it ;)
    – Izzumi
    Nov 20 at 10:11















up vote
4
down vote



accepted







up vote
4
down vote



accepted






you need to import User in classes_2.py :



from classes_1 import User

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())

class Admin(User):
def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()





share|improve this answer












you need to import User in classes_2.py :



from classes_1 import User

class Privileges():
def __init__(self):
self.privileges = [
'access to add msg',
'access to delete users',
'access to ban users',
]


def show_privileges(self):
print('Hi Admin, your privileges is: ')
for kk in self.privileges:
print('n- ' + kk.lower())

class Admin(User):
def __init__(self, first_name, last_name, location='', field=''):
super().__init__(first_name, last_name, location, field)
self.privilege = Privileges()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 17:09









Corentin Limier

1,919159




1,919159












  • Sorry, I forgot to specify conditions.. (One of the conditions is to separate the parent class from the child! That is, everything good with their location [classes_1.py, classes_2.py])
    – Izzumi
    Nov 19 at 17:24












  • @Izzumi ok, you need to import it then. You should know, btw, that this isn't a common pattern in Python, that is, all classes in separate files.
    – juanpa.arrivillaga
    Nov 19 at 17:31












  • @Izzumi Not sure to understand. Aren't they already separated ? (Admin is in classes_2, User in classes_1 ?). Or do you mean that you want to specify later which class Admin inherits ? (in n10_12.py)
    – Corentin Limier
    Nov 19 at 18:02










  • @juanpa.arrivillaga Thanks I got it ;)
    – Izzumi
    Nov 20 at 10:11




















  • Sorry, I forgot to specify conditions.. (One of the conditions is to separate the parent class from the child! That is, everything good with their location [classes_1.py, classes_2.py])
    – Izzumi
    Nov 19 at 17:24












  • @Izzumi ok, you need to import it then. You should know, btw, that this isn't a common pattern in Python, that is, all classes in separate files.
    – juanpa.arrivillaga
    Nov 19 at 17:31












  • @Izzumi Not sure to understand. Aren't they already separated ? (Admin is in classes_2, User in classes_1 ?). Or do you mean that you want to specify later which class Admin inherits ? (in n10_12.py)
    – Corentin Limier
    Nov 19 at 18:02










  • @juanpa.arrivillaga Thanks I got it ;)
    – Izzumi
    Nov 20 at 10:11


















Sorry, I forgot to specify conditions.. (One of the conditions is to separate the parent class from the child! That is, everything good with their location [classes_1.py, classes_2.py])
– Izzumi
Nov 19 at 17:24






Sorry, I forgot to specify conditions.. (One of the conditions is to separate the parent class from the child! That is, everything good with their location [classes_1.py, classes_2.py])
– Izzumi
Nov 19 at 17:24














@Izzumi ok, you need to import it then. You should know, btw, that this isn't a common pattern in Python, that is, all classes in separate files.
– juanpa.arrivillaga
Nov 19 at 17:31






@Izzumi ok, you need to import it then. You should know, btw, that this isn't a common pattern in Python, that is, all classes in separate files.
– juanpa.arrivillaga
Nov 19 at 17:31














@Izzumi Not sure to understand. Aren't they already separated ? (Admin is in classes_2, User in classes_1 ?). Or do you mean that you want to specify later which class Admin inherits ? (in n10_12.py)
– Corentin Limier
Nov 19 at 18:02




@Izzumi Not sure to understand. Aren't they already separated ? (Admin is in classes_2, User in classes_1 ?). Or do you mean that you want to specify later which class Admin inherits ? (in n10_12.py)
– Corentin Limier
Nov 19 at 18:02












@juanpa.arrivillaga Thanks I got it ;)
– Izzumi
Nov 20 at 10:11






@juanpa.arrivillaga Thanks I got it ;)
– Izzumi
Nov 20 at 10:11














up vote
0
down vote













I think your second classes file should be importing the first classes file;otherwise, how does it know what User is?






share|improve this answer





















  • such a condition was given in the assignment, so it should be
    – Izzumi
    Nov 19 at 17:28















up vote
0
down vote













I think your second classes file should be importing the first classes file;otherwise, how does it know what User is?






share|improve this answer





















  • such a condition was given in the assignment, so it should be
    – Izzumi
    Nov 19 at 17:28













up vote
0
down vote










up vote
0
down vote









I think your second classes file should be importing the first classes file;otherwise, how does it know what User is?






share|improve this answer












I think your second classes file should be importing the first classes file;otherwise, how does it know what User is?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 17:11









Lulz

11




11












  • such a condition was given in the assignment, so it should be
    – Izzumi
    Nov 19 at 17:28


















  • such a condition was given in the assignment, so it should be
    – Izzumi
    Nov 19 at 17:28
















such a condition was given in the assignment, so it should be
– Izzumi
Nov 19 at 17:28




such a condition was given in the assignment, so it should be
– Izzumi
Nov 19 at 17:28


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379518%2fcant-import-python-classes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?