Can't import a class from another python file - python

I searched on internet but I still don't understand why my code doesn't work. I have a Python file called Era.py, in this file I have this class:
class input:
def __init__(self, username):
self.username = username
close = ["X", "x"]
print("So, let's start, sweetheart, press X when you want to stop. \n")
user_input = input("")
user_input = user_input.upper()
while user_input not in close:
user_in = Subject(username, user_input)
user_input = input("")
print("Good bye, sweetheart!")
And in another Python file, read_from_database.py I have this:
class oldUser:
def __init__(self, login, password):
self.login = login
self.password = password
results = "SELECT * FROM users WHERE username = '" + login + "'" + " AND password = " + "'" + password + "'"
mycursor.execute(results)
results = mycursor.fetchall()
if not results:
print("User don't exist or wrong login details! \n")
print(add_to_database)
else:
print("Login successfully!")
from Era import input
execute = input(login)
But when I try to import from Era, input, I have this error:
ImportError: cannot import name 'input' from 'Era'
All files are in the same folder and theoretically it should work, 1 week ago it worked

The problem here is that you are overriding the built-in input function of Python, while still trying to use it.
You can see this in the input class of your Era.py file.
This class attempts to use the built-in input function for collecting user_input.
Because of this, attempting to from Era import input will occasionally fail, as Python gets confused about what to do.
Bottom line here is that you should never override any built-in functions (especially if you need them as well) unless you know what you are doing.
To add to this, it is common in Python that class names start with a capital letter and use no underscores, while functions and modules (like Era here) have only lowercase letters and underscores.
So, change your code to the following:
# era.py
class Input:
def __init__(self, username):
self.username = username
close = ["X", "x"]
print("So, let's start, sweetheart, press X when you want to stop. \n")
user_input = input("")
user_input = user_input.upper()
while user_input not in close:
user_in = Subject(username, user_input)
user_input = input("")
print("Good bye, sweetheart!")
# read_from_database.py
from era import Input
class OldUser:
def __init__(self, login, password):
self.login = login
self.password = password
results = "SELECT * FROM users WHERE username = '" + login + "'" + " AND password = " + "'" + password + "'"
mycursor.execute(results)
results = mycursor.fetchall()
if not results:
print("User don't exist or wrong login details! \n")
print(add_to_database)
else:
print("Login successfully!")
execute = Input(login)
You also may want to have a look at your Input class, as it can be simplified in several ways.

Related

How can I run this code and how can I improve this code?

import sys as s
import datetime
class LoginPage:
def __int__(self, username, password):
self.username = username
self.password = password
self.incorrectPass = 0
def loginProgrma(self):
while True:
while True:
nameInput = input("Enter your username: ➸")
if nameInput != self.username:
print("\t(Username is incorrect. Re-enter your username)")
elif nameInput == self.username:
print("\t(Enter your password)")
break
while True:
passInput = input("Enter your password: ➳")
if passInput != self.password:
self.incorrectPass += 1
print("\t(Incorrect password. Re-enter your password)")
if self.incorrectPass == 3:
s.exit('\nYou have been locked!')
elif passInput == self.password:
break
time = datetime.datetime.now()
print('You logged in', time)
return f"\n\t(Welcome back {nameInput}.)"
So I'm trying to make a short login program where where the program ask to enter the username and password. The problem I don't know how can I run this code, I tried by calling the class LoginPage and the pass 2 arguments(username, password) which is mot working I tried to find the solution on the internet but I couldn't find.
And how can I improve this code?.
You have typed __int__ instead of __init__. (You forgot an extra i) If you fixed that, you can after you called the class LoginPage call the loginProgrma function.
First of all remove the typo in __init__ from __int__ to __init__
Anyway there seem to be some typos in your code but to run the method you showed create an instance of the class and call the function like this:
#create instance
login_page = LoginPage(username='username', password='password')
#call method
login_page.loginProgrma()`
This should probably work

How to append to csv file before script is done running?

I am currently making a log in system which stores usernames and passwords in a text file.
This is my code:
import csv
Brugere = open("D:\Filer\Programmering/Profiles.txt","r+",newline="\n")
writer = csv.writer(Brugere, delimiter=",")
print("Welcome to the chat app!")
Brugernavn = "" //means username
Kodeord = "" // means password
def Signup(j,k):
print("Welcome to sign up!\n Please enter your Username")
j = input("")
if j not in Brugere:
print("Hello, " + j + ", please enter a password:")
k = input("")
line1 = [j,k]
writer.writerow(line1)
print("Great! Now you can sign in")
Signin(Brugernavn, Kodeord)
else:
print("Username already taken! try again")
Signup(Brugernavn, Kodeord)
def Signin(U,P):
print("Welcome to sign in!")
print("Do you already have an account?[y/n]")
ans = input("")
if ans == "n":
print("You will now be redirected to sign up")
Signup(Brugernavn, Kodeord)
elif ans == "y":
U = input("Username: ")
if U in Brugere:
P = input("Password: ")
print("WELCOME")
else:
print("Invalid username, try again")
Signin(Brugernavn, Kodeord)
else:
print("Please write 'y' or 'n'")
Signin(Brugernavn, Kodeord)
Signin(Brugernavn, Kodeord)
Brugere.close()
When I run it, the signup function works as it should, but when the signin function is called, it can't find the username and password from the text file. I think it's because they only get appended after the script is done running. However, 'Im not sure.
I've been struggling for a long time with this csv file thing. I want to have it like
this where each line is a list where I can find the username and password
I've heard people calling it "comma seperated values", however I have no idea how to do it.
TL;DR:
I suspect the simplest solution is:
...
if j not in brugere:
...
writer.writerow(line1)
brugere.flush() # Flush pending changes to file.
...
...
However, I've noticed a few things worthy of consideration:
Python help. You can get help on Python things within an interactive Python session:
python
> import csv
> help(csv)
CSV for this sort of usecase isn't a good idea. If it's production grade use a proper DB, if you're just playing around locally to get a feel for Python use a dict() and store as JSON. I.e.:
import json
username = "myuser"
password = "password"
filename = "data.json"
# Reads data. Requires file `data.json` to exist, with content `{}`.
with open(filename, "r") as data_stream:
data = json.load(data_stream)
if username in data:
print(f"User {username} exists.")
else:
data[username] = password # You'd usually hash and salt a password. It's a separate topic.
# Writes data to disk.
with open(filename, "w") as data_stream:
data_stream.write(json.dumps(data, indent=2)) # Makes it look pretty on disk.
Note that open() returns a stream wrapped in class io.TextIOWrapper (see python -c "import io; help(io.TextIOWrapper)"). It's basically an interface to get the data you want, not the data itself. Try open("test.txt").readlines() to actually read data from stream, for example. Note that streams are consumables.
When you call in on the stream Brugere you're calling the __contains__() dunder method on class io.TextIOWrapper which isn't implemented. But Python being Python it returns False (it's not meaningful).
The CSV interfaces are pretty straightforward:
import csv
filename = "test.txt"
f = open(filename, "w")
writer = csv.writer(f)
writer.write(["myuser", "password"])
# Flushes lines to file.
f.flush()
...
f.close()
Creating a CSV is as simple as writing a string with each value separated by a designated character, often a comma as the name implies. You don't really need the csv library for that.
Your Signup method would then look as follows:
def Signup(j,k):
print("Welcome to sign up!\n Please enter your Username")
j = input("")
if j not in Brugere:
print("Hello, " + j + ", please enter a password:")
k = input("")
line1 = [j,k]
# Write a string with a comma between the values
Brugere.write("{}, {}\n".format(j, k))
print("Great! Now you can sign in")
Signin(Brugernavn, Kodeord)
else:
print("Username already taken! try again")
Signup(Brugernavn, Kodeord)
With that being said, using CSVs to store operational data in a an application does not make all that much sense to me. Please see below for a modified version of signup using SQLite, which is much more robust and still simple to use:
import sqlite3
con = sqlite3.connect('brugere.db')
cur = con.cursor()
# Create a user table with bruger and kodeord
# I added an extra field for when the user was created as well
# which defaults to the date and time the user was created
cur.execute("""create table if not exists brugere
(brugernavn text,
kodeord text,
dato_oprettet TIMESTAMP DEFAULT CURRENT_TIMESTAMP)""")
con.close()
def signup():
con = sqlite3.connect('brugere.db')
cur = con.cursor()
with con:
print("Please enter your Username")
bruger = input()
cur.execute("select brugernavn from brugere where brugernavn =?", [bruger])
bruger_eksisterer = cur.fetchone()
if bruger_eksisterer:
print("Username already taken! Try again")
# Signup(Brugernavn, Kodeord)
else:
print("Hello, " + bruger + ", please enter a password:")
kodeord = input()
try:
cur.execute("insert into brugere (brugernavn, kodeord) values (?, ?)", (bruger, kodeord))
print("Great! Now you can sign in")
except sqlite3.Error as Err:
print("Couldn't add user.\nError: {}".format(Err))
# Signin(Brugernavn, Kodeord)
con.close()
signup()
Modifying the signin should be straighforward, but I'll leave that part up to you.

need help in simple multiple login in python

i am trying to make a simple login system for python. i tried several things using break,return. but i cant get pass the login system using the second line in the text file onwards. i can only login through the first line of the text file, i took the code fro an other question but didnt know how to get it to work so i tried to change it so i could understand how it can work. I am extremely new to python. please let me know where i got wrong and how i can change it to get it to work!
format for user.txt is
first name|last name|occupation|id|username|password
John|goh|worker|1|admin|1234
import datetime
def check():
x = 0
users = open("users.txt").read().split("\n")
for i in range(len(users)): users[i] = users[i].split("|")
while (x < 3):
username = str(input("Username: \n"))
password = str(input("Password: \n"))
f = open('project_AI.txt', 'a')
f.write(str(datetime.now()))
f.write('\n' + username + '\n')
f.write(password + '\n')
f.close()
for user in users:
uname = user[4]
pword = user[5]
if uname == username and pword == password:
print("Hello " + user[1] + ".")
print("You are logged in as: " + user[2] + '.')
x += 3
else:
print('error')
check()
x += 1
return
check()
many thanks!!
I think the "return" in the penultimate line is at the wrong indentation, and isn't really needed at all.
As soon as python touches a return, it instantly destroys the function. In your code, after every user it checks, it hits the return. Meaning it will never even reach the second user.
You also want to not use check() within the function, as it will create a clone of itself within itself. The while x < 3 will go through the logic multiple times for you.
import datetime
def check():
x = 0
users = open("users.txt").read().split("\n")
for i in range(len(users)): users[i] = users[i].split("|")
while (x < 3):
username = str(input("Username: \n"))
password = str(input("Password: \n"))
f = open('project_AI.txt', 'a')
f.write(str(datetime.now()))
f.write('\n' + username + '\n')
f.write(password + '\n')
f.close()
for user in users:
uname = user[4]
pword = user[5]
if uname == username and pword == password:
print("Hello " + user[1] + ".")
print("You are logged in as: " + user[2] + '.')
x += 3
return
print('error')
x += 1
check()

Python Login and Register System using text files

Hey I am trying to create a system using text files where a user can sign up and log in. All the data will be stored in plain text in a text file called User_Data.txt. My code works but I would like to know if there is anything I missed or If I could improve it in any way. Sorry for the Bad code Formatting in advance.
def choices():
print("Please choose what you would like to do.")
choice = int(input("For Sigining Up Type 1 and For Signing in Type 2: "))
if choice == 1:
return getdetails()
elif choice == 2:
return checkdetails()
else:
raise TypeError
def getdetails():
print("Please Provide")
name = str(input("Name: "))
password = str(input("Password: "))
f = open("User_Data.txt",'r')
info = f.read()
if name in info:
return "Name Unavailable. Please Try Again"
f.close()
f = open("User_Data.txt",'w')
info = info + " " +name + " " + password
f.write(info)
def checkdetails():
print("Please Provide")
name = str(input("Name: "))
password = str(input("Password: "))
f = open("User_Data.txt",'r')
info = f.read()
info = info.split()
if name in info:
index = info.index(name) + 1
usr_password = info[index]
if usr_password == password:
return "Welcome Back, " + name
else:
return "Password entered is wrong"
else:
return "Name not found. Please Sign Up."
print(choices())
There is a lot of improvements You could do.
First of all, split functionality to smaller function.
PASSWORD_FNAME = "User_Data.txt"
def get_existing_users():
with open("r", PASSWORD_FNAME ) as fp:
for line in fp.readlines():
# This expects each line of a file to be (name, pass) seperated by whitespace
username, password = line.split()
yield username, password
def is_authorized(username, password):
return any((user == (username, password) for user in get_existing_users())
def user_exists(username):
return any((usr_name == username) for usr_name, _ in get_existing_users())
# above is equivalent of:
#
# for usr_name, _ in get_existing_users():
# if usr_name == username:
# return True
# return False
def ask_user_credentials():
print("Please Provide")
name = str(input("Name: "))
password = str(input("Password: "))
return name, password
def checkdetails():
name, password = ask_user_credentials()
if is_authorized(name, password):
return "Welcome Back, " + name
if user_exists(name):
return "Password entered is wrong"
return "Name not found. Please Sign Up."
def getdetails():
name, password = ask_user_credentials()
if not user_exists(name):
return "Name Unavailable. Please Try Again"
# Not sure tho what would You like to do here
It's always good to remember to always close your file if you read it.
So if you do something like:
f = open("r", "file.txt") remember to always call f.close() later.
If you use context manager and do it like:
with open("r", "file.txt") as fp:
print(fp.read())
it will automatically close the file for you at the end.
Firstly, fix the spelling error at int(input("For Sigining Up Type 1") Other than that I would add some kind of purpose, for example storing secret numbers or something.
For example you can extend your script with a simple password recovery system.
I think it could be useful to learn...
You can implement a sort of a simple hashing system in order to avoid saving the password as plain text.
If you want to add a GUI, please consider using Tkinter.
https://docs.python.org/3/library/tkinter.html
Let we know.
Good Luck and Keep Coding with <3

Issues with my Python login script. Invalid syntax on try

I am having issues with the current program that I am trying to write. I don't understand why it keeps saying this or why.
Also can this code be extended to cover IP logging and making sure multiple users can be logged in on the same IP in theory?
Here is the code:
import hashlib
import time
#cPickle is faster then pickle but not available in all python releases
#thats why i used a try/accept there
try: import cPickle as cp
#load the database if it exist, if not it create one
try:
f =(r"C:\Users\Owner\Desktop\python\database.data")
data = cp.load(f)
except IOError:
data = {}
#A simple function made to make data dumping easy
def easyDump(data_):
f = file(r"C:\Users\Owner\Desktop\python\database.data", "w")
cp.dump(data_, f)
f.close()
#Get's the date (We'll use this as the custom salt)
def getData():
return str(time.strftime("%d-%m-%Y"))
#A function which accepts two parameters, password and date.
#The date is the custom salt. It returns the sha512 hash excetpyion
def salt(password, date):
salted = hasglib.sha512(password + str(data)).hexdigest()
retun str(salted)
menu = """"
1.Login
2.Register
3.Exit
"""
while True:
print menu
choice = int(raw_input("Your choice please: "))
if choice ==1:
username = raw_input("Enter your username please: ")
password = raw.input("Enter your authentication code please: ")
#if the username is found on the database
if data.has_key(username):
#date is equal to our secured stored data
date = date[username][1]
#check of the given password + date is equal to what is stored on the database
#password
if salt(password, date) == date[username][0]:
print"Welcome %s!" % username
else:
print "Incorrect password"
else:
print "user %s not found, please register!" % username
elif choice == 2:
username = raw_input("Please enter yout username: !")
password = raw_input("Please enter your password: !")
#if username exists in the system already then the name is taken
if data.has_key(username):
print "user %s already registered, please put in another % username
else:
#in order words data = {username: hash, date}
data[username] = [salt(password, getData()), get Data()]
easyDump(data)
print "user %s successfully registereed!" %username
elif choice == 3:
print "goodbye!"
break
else:
print "invaid input or commands"
This code:
try: import cPickle as cp
is not followed by an except ... hence the syntax error
Your code contains many indenting, syntax errors and spelling mistakes. The following fixes these to allow it to at least run:
import hashlib
import time
#cPickle is faster then pickle but not available in all Python releases
#That is why I used a try/accept there
try:
import cPickle as cp
except:
import pickle as cp
#load the database if it exist, if not it create one
try:
f = open(r"C:\Users\Owner\Desktop\python\database.data")
data = cp.load(f)
except IOError:
data = {}
#A simple function made to make data dumping easy
def easyDump(data_):
f = file(r"C:\Users\Owner\Desktop\python\database.data", "w")
cp.dump(data_, f)
f.close()
#Get's the date (We'll use this as the custom salt)
def getData():
return str(time.strftime("%d-%m-%Y"))
#A function which accepts two parameters, password and date.
#The date is the custom salt. It returns the sha512 hash exception
def salt(password, date):
salted = hasglib.sha512(password + str(data)).hexdigest()
return str(salted)
menu = """"
1.Login
2.Register
3.Exit
"""
while True:
print menu
choice = int(raw_input("Your choice please: "))
if choice == 1:
username = raw_input("Enter your username please: ")
password = raw.input("Enter your authentication code please: ")
#if the username is found on the database
if data.has_key(username):
#date is equal to our secured stored data
date = date[username][1]
#check of the given password + date is equal to what is stored on the database
#password
if salt(password, date) == date[username][0]:
print"Welcome %s!" % username
else:
print "Incorrect password"
else:
print "user %s not found, please register!" % username
elif choice == 2:
username = raw_input("Please enter yout username: !")
password = raw_input("Please enter your password: !")
#if username exists in the system already then the name is taken
if data.has_key(username):
print "user %s already registered, please put in another" % username
else:
#in order words data = {username: hash, date}
data[username] = [salt(password, getData()), getData()]
easyDump(data)
print "user %s successfully registered!" % username
elif choice == 3:
print "goodbye!"
break
else:
print "invalid input or commands"
Indenting in Python is very important, getting it wrong can completely change the meaning of the code.

Categories