简体   繁体   中英

Run a Python script without the IDE

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

感谢@abarnert,解决方案是使用Pyinstaller,它使我可以将代码“冻结”为exe文件。

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.

eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM