Python Forum
Importing a .py file with a value of a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing a .py file with a value of a variable
#1
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.
Reply
#2
it's done as in this example:
>>> from importlib import import_module
>>> tikky = 'tkinter'
>>> tk = import_module(f"{tikky}")
>>> root = tk.Tk()
Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 721 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  Problem with importing Python file in Visual Studio Code DXav 7 5,066 Jun-15-2022, 12:54 PM
Last Post: snippsat
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,365 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Importing a function from another file runs the old lines also dedesssse 6 2,541 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  Importing text file into excel spreadsheet with formatting david_dsmn 1 3,606 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  importing a CSV file into Python russoj5 1 2,945 Aug-02-2020, 12:03 AM
Last Post: scidam
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,097 Jun-29-2020, 08:51 PM
Last Post: buran
  importing CSV file into a OOP Class table using Python faruk61 1 2,951 Apr-15-2020, 12:00 PM
Last Post: faruk61
  importing CSV file into a HTML table using Python trybakov 1 2,278 Feb-22-2020, 09:47 PM
Last Post: scidam
  Importing variables from another file IILawrenceII 7 9,996 Jan-18-2020, 12:31 PM
Last Post: IILawrenceII

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020