Mar-22-2025, 07:56 PM
Hello,
I'm just starting my adventure with Python, so please bear with me.
I wrote a script that deletes a database.
I wanted the script to be run using batch. That's why I decided to generate an .exe file using Pyinstaller.
Unfortunately, when I try to run the .exe I get errors.
My code below:
delete_orpheus_db.py
config.py
config.json
I call this line in CMD in folder with my delete_orpheus_db.py
And here is error which I got after run .exe file
I think that I forgot about something but I have no idea what it is.
Thanks a lot for any help!
Have a good day
I'm just starting my adventure with Python, so please bear with me.
I wrote a script that deletes a database.
I wanted the script to be run using batch. That's why I decided to generate an .exe file using Pyinstaller.
Unfortunately, when I try to run the .exe I get errors.
My code below:
delete_orpheus_db.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import mysql.connector from mysql.connector import Error from config import get_config def delete_orpheus_database(): try : connection = mysql.connector.connect( host = get_config()[ 'host' ], user = get_config()[ 'user' ], password = get_config()[ 'password' ] ) if connection.is_connected(): cursor = connection.cursor() cursor.execute( "DROP DATABASE orpheus" ) print ( "Database 'orpheus' successfully deleted." ) except Error as e: print ( f "Test connection failed. Error: {e}" ) finally : if connection.is_connected(): cursor.close() connection.close() print ( "Database connection closed." ) |
1 2 3 4 5 6 7 |
import json def get_config(): with open ( 'C:/Repositories/ORPHEUS/DB/CONFIG/config.json' , "r" ) as config_file: config = json.load(config_file) return config |
1 2 3 4 5 6 |
{ "host" : "localhost" , "user" : "root" , "password" : "11111111" , "database" : "orpheus" } |
1 |
pyinstaller - - onefile delete_orpheus_db.py |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
C:\Users\mnawr>delete_orpheus_db.bat Traceback (most recent call last): File "delete_orpheus_db.py" , line 9 , in delete_orpheus_database File "mysql\connector\pooling.py" , line 322 , in connect File "mysql\connector\connection_cext.py" , line 153 , in __init__ File "mysql\connector\abstracts.py" , line 1529 , in connect File "mysql\connector\connection_cext.py" , line 365 , in _open_connection RuntimeError: Failed raising error. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "delete_orpheus_db.py" , line 29 , in <module> File "delete_orpheus_db.py" , line 24 , in delete_orpheus_database UnboundLocalError: cannot access local variable 'connection' where it is not associated with a value [PYI - 27856 :ERROR] Failed to execute script 'delete_orpheus_db' due to unhandled exception! |
Thanks a lot for any help!
Have a good day