0

I am trying to create a python executable using below code.

import pandas as pd
import pyodbc as po
import os
#variables
server = 'DESKTOP-9B94UPJ' 
database = 'DB1' 
username = 'etluser' 
password = 'password' 
rawfile_directory='E:\\Projects\\Ebay\\ETLApp\\'
cnxn = po.connect('DRIVER={SQL 
Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

df=pd.read_csv(rawfile_directory+"SPSS_Files\\"+filename,usecols=var_filteredCols)

cnxn = po.connect('DRIVER={SQL 
Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
     sql_stmt = "INSERT INTO "+var_table+" ("+var_DBCols+") values("+var_placeholder+")"
     cursor = cnxn.cursor()
     cursor.fast_executemany = True
     cursor.executemany(sql_stmt, df.values.tolist())
           # print(f'{len(df)} rows inserted to the {MY_TABLE} table')
     cursor.commit()
     cursor.close()
     cnxn.close()

I have created an exe using pyinstaller

pyinstaller --onefile etl.py

I want to pass the database credential and the folder path on the runtime to the exe. Can any of experts advise me how to achieve this? Thanks in advance

Anoop M
  • 11
  • 3

1 Answers1

0

If you don't want to create a GUI program that uses a user interface to extract credentials, you are probably left with command-line manipulation. You can easily do the above by passing the arguments in and then extracting them using sys.argv

For example:

import sys
if len(sys.argv) < 6:
    raise ValueError("Not enough commandline arguments! Please run with <executable> <server> <database> <username> <password> <folder_path>")

server = sys.argv[1]
database = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]
rawfile_directory= sys.argv[5]

Then you can run your executable from command prompt using something like

C:\> <EXEfilename> DESKTOP-9B Ebay_CX etluser password E:\Projects\Ebay\ETLApp

If you want a fancier, more polished commandline interface, consider using argparse so you can give your end users more help on how to use the different arguments to your executable, e.g. with usage instructions, --options instead of args, --help support, etc.

You could also ask for user input during runtime, e.g. after running the executable with input() but this is less standard

See also: after compiling python program, how to input arguments

lemonhead
  • 5,328
  • 1
  • 13
  • 25