Python Forum

Full Version: Importing a .py file with a value of a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I am trying to make an ATM in the CMD with the use of Python but I have to import a module which could have any name because it is being created through user input.

I basically just want to import a module with the use of a variable.

I already have this:

import os 
import time
import importlib

users = {}
global username

class login_system:
	
	def registration():
		username = input("Select your username: ")

		if username in users:
			print("This username is already in use.")

			registration()

		elif username not in users:
			password = input("Select your password: ")
			users[username] = password

			save = open(f"{username}.py","w+")
			save.write("money = 0.00")
			save.close()

			print("Registered successfully.")
			login_system.logging_in()

	def logging_in():
		username = input("Enter your username: ")

		if username not in users:
			print("Username not found.")
			login_system.registration()

		elif username in users:
			password = input("Enter your password: ")

			if password == users[username]:
				print("Successfully logged in.")
                                         #<-- so right here I would want to import it
				bank_system.options()

	def log_or_reg():
		choice = input("Would you like to 'LOG IN' or 'REGISTER'?\n:")
		choice = choice.upper()

		if choice == 'REGISTER':
			os.system('cls')
			login_system.registration()

		elif choice == 'LOG IN' or choice == 'LOGIN':
			os.system('cls')
			login_system.logging_in()

		else:
			print("Wrong input, please try again.")
			time.sleep(2)
			os.system('cls')
			login_system.log_or_reg()


class bank_system():
	
	def options():
		option = input("Do you wish to see 'BALANCE','DEPOSIT','WITHDRAW' or 'LOG OUT?'\n:")
		option = option.upper()

		if option == "LOG OUT" or option == "LOGOUT":
			login_system.log_or_reg()

		elif option == "DEPOSIT":
			depo_amount = input("Enter deposit amount: ")
			if float(depo_amount) < 0:
				print("You can't deposit less than 0 dollars.")
			elif float(depo_amount) > 0:
				money = money + float(depo_amount)
				print(money)

print("Welcome to Sk4rdBank!")
time.sleep(3)
os.system('cls')

login_system.log_or_reg()
If anyone of you fantastic people could help me I would be very happy.
it's done as in this example:
>>> from importlib import import_module
>>> tikky = 'tkinter'
>>> tk = import_module(f"{tikky}")
>>> root = tk.Tk()
Sorry, but this is wrong on so many levels.
1. You shouldn't store the information as python modules. Larz answer is of course correct with respect to your question, but the problem is the design itself. Store information as csv file, json file, database (e.g. sqlite support comes with python standard library) or any of many other options.
2. your classes, are not really classes. OK, technically you use the word class when you define them. But they are in fact just another layer over regular functions. If you remove all the reference to login_system or bank_system your code will work all the same. The idea is that class is blueprint of an object. You can create as many instances of a class as you wish. To be a class it has to have access to instance and/or class itself. Methods would get a class instance as first argument (by convention we use self for that) You really need to get back to read about class basics/fundamentals.
3. Don't use global variables. You pass arguments to functions/methods and/or use class properties.