简体   繁体   English

在没有IDE的情况下运行Python脚本

[英]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. 我有一个编写的python脚本,使用tkinter和panda进行操作:在桌面上选择一个CSV文件,导入该数据,执行一些操作,将创建的新数据帧导出到新的csv中。

Is there a way to run the program without the person needing to open up a python IDE and run it from there? 有没有一种方法可以运行该程序而无需他人打开python IDE并从那里运行它?
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. 当前,当我尝试单击并运行tester.py程序时,我看到cmd_line(终端)框短暂打开,然后关闭,而没有我的tkinter提示或其他任何内容。

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? 有没有一种方法可以让我设置此程序,让他们单击exe文件或bat文件,然后运行脚本,收集所需的用户输入,并按我的要求输出csv文件?

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. 创建一个.bat文件,您需要在其中执行命令来执行python文件。

eg c:\\python27\\python.exe c:\\somescript.py %* 例如c:\\python27\\python.exe c:\\somescript.py %*

For reference look here 供参考,请看这里

MacOS / Linux MacOS / Linux

Create a .sh file which can be executed from your shell/terminal or by double clicking its sym link. 创建一个.sh文件,该文件可以从您的Shell /终端执行,也可以通过双击其符号链接来执行。
Your .sh file will look like 您的.sh文件看起来像

#!/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. 另外,您可以创建指向您的python文件的符号链接并使之可执行。 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. 如果仅使用名称代替PATH_OF_SYMLINK ,它将在您当前的目录中创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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