2

I have a python script I wrote which uses tkinter and panda to: choose a CSV file on the desktop, imports in that data, does some stuff, exports the new dataframe created into a new csv.

Is there a way to run the program without the person needing to open up a python IDE and run it from there?
Currently when I try to just click and run the tester.py program I see the cmd_line (terminal) box open briefly and then close without my tkinter prompt or anything else.

My goal, or my ideal is that I wrote this program to help automate some tasks for non-technical coworkers. Is there a way that I could set up this program to just have them click on an exe file or a bat file and for the script to run, collect the User Input needed, and output the csv file like I want it to?

I've done some brief google searching but I haven't been able to find a clear answer.

import tkinter
import csv
import pandas as pd    

from tkinter import Tk
from tkinter.filedialog import askopenfilename

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)

df1 = pd.read_csv(filename)

df2 = df1.query("STATE != 'NY'") # stores records not in NY

df3 = df1[df1["FIRST_NAME"].str.contains(" ", regex=True)] # stores records that have a space in the first name

dferror = [df2, df3]
dferror = pd.concat(dferror).drop_duplicates().reset_index() # merges dataframes, drops duplicates


dferror.to_csv("C:\errors.csv")

edited to add my import clauses

Kalgaer
  • 33
  • 1
  • 1
  • 6
  • 1
    Your program can already be run from the shell (e.g., `bash` in `Terminal.app` on macOS, or `cmd` on Windows, etc.). If you want it to be runnable from the GUI… well, you appear to be on Windows, so it's a matter of configuring your Windows Explorer to open `.py` files by running them with Python, instead of by editing them in the IDE. Which is really a question for Super User, not for Stack Overflow. – abarnert Jul 20 '18 at 18:55
  • Alternatively, you can use something like `py2exe` or `PyInstaller` or `cx_Freeze` to create a runnable `.exe` application out of your script. Then, people can double-click it in Explorer and it'll run your program. You can also make it include all of Python inside that `.exe`, so your coworkers don't even need to have Python installed. If that's the way you want to go, look for tutorials for those three projects (and maybe look for alternatives to them and look up tutorials for those alternatives), skim through them, and see which one you want to learn and use. – abarnert Jul 20 '18 at 18:58
  • @Marcus.Aurelianus How is scheduler going to help here? – abarnert Jul 20 '18 at 18:59
  • Run it from a command prompt, so that you have a chance to see the error message - which is probably going to be something like "name 'pd' not found", since you forgot to import all the modules you're using. – jasonharper Jul 20 '18 at 19:39
  • Maybe I need to rephrase a bit, and it is totally possible I am asking this question on the wrong forum. But my goal is to have this program be run purely off of GUI (no going into the cmd line). I'm not worried about needing to see errors as I have already done my testing and it is working the way as intended. I wan't my non-technical coworker to be able to click on my .py file or a .exe file and for my program to run. Is that more clear? – Kalgaer Jul 20 '18 at 20:09
  • @abarnert just re read through your second comment, that looks exactly what i need. I will look into it, thank you so much! – Kalgaer Jul 20 '18 at 20:24

2 Answers2

1

Thanks to @abarnert, the solution was to use Pyinstaller which allows me to "freeze" the code into an exe file

Kalgaer
  • 33
  • 1
  • 1
  • 6
1

You can write a small executable script based upon your OS

Windows

Create a .bat file in which you need there needs to the command to execute your python file.

e.g. c:\python27\python.exe c:\somescript.py %*

For reference look here

MacOS / Linux

Create a .sh file which can be executed from your shell/terminal or by double clicking its sym link.
Your .sh file will look like

#!/bin/bash

python PATH_TO_YOUR_PYTHON_FILE

Then you must make it executable via running the following in terminal

chmod u+x PATH_TO_.SH_FILE

Alternatively you can create a symbolic link to your python file and make it executable. This symlink will be executable by double click. To create a symlink:

ln -sf PATH_TO_.SH_FILE PATH_OF_SYMLINK

If you put just a name in place of PATH_OF_SYMLINK it will be created in your present directory.

fireball.1
  • 1,413
  • 2
  • 17
  • 42
  • I've spent the last 2 work days trying to compress ("freeze" i believe is the correct jargon) my python script into a .exe so it could be easily run. I have had 0 luck with it (keep getting errors with pyinstaller). I am definitely going to go with the .bat file. – Kalgaer Jul 23 '18 at 22:33
  • Hello did everything here for mac and when I got to the chmod u+x PATH_TO_.SH_FILE part it didn’t work. I put in my own path but it didn’t work. Can you help? – Pixeled Dec 07 '21 at 13:33
  • @JustCoookie Can you please put down the steps you took and the error you faced ? – fireball.1 Dec 07 '21 at 14:09
  • @fireball.1 First I made a new file called `Calculator.sh`. Then in that file, I put `/Users/cooper/Desktop/Calculator/Calculator.py`. Then in the terminal of the program I put `chmod u+x /Users/cooper/Desktop/Calculator/Calculator.sh` and nothing happened – Pixeled Dec 07 '21 at 21:46
  • Your file contents should be `python /Users/cooper/Desktop/Calculator/Calculator.py`. After making this change, just clicking the `Calculator.sh` file should be enough on any system with python installed. – fireball.1 Dec 08 '21 at 07:10
  • @fireball.1 Is there a way to make it run when I click the file? It does not run when I click the file it just opens it. Also will people without python installed be able to run it? Because that's what I'm aiming for. – Pixeled Dec 08 '21 at 22:25