是否可以创建可在任何地方运行的,可分发给Windows的Python程序? [英] Is it possible to create a Python program distributable for Windows that can be run from anywhere?

查看:105
本文介绍了是否可以创建可在任何地方运行的,可分发给Windows的Python程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的问题用词表达得很不好,这也许就是为什么我还没有找到答案的原因.

I think my question is quite badly phrased, which may be why I haven't been able to find the answer yet.

我用Python创建了程序,并使用bdist_winisnt从中创建了一个可安装的.exe文件.安装该程序后,我希望能够在任何地方运行它.这是一个命令行程序,所以我希望用户能够位于另一个目录中,并且仍然能够在命令行中键入example.py,并且程序可以运行.

I created my program in Python, created an installable .exe file from it using bdist_winisnt. Once the program is installed, I would like to be able to run it from anywhere. It's a command line program, so I would like the user to be able to be in a different directory and still be able to type example.py in command line and the program can run.

这可能吗?是否有办法在setup.py中包含某种路径指令,该指令将在安装时运行,以便计算机始终知道它在哪里?

Is this possible? Is there a way of including some kind of path instruction in the setup.py which will be run on install so that the computer will always know where it is?

我也希望能够在Linux上做到这一点,它会一样工作吗?

I would also like to be able to do this in Linux at some point, will it work the same?

我对编程很陌生,所以我可能对我所说的话犯了一些错误,请事先道歉.

I'm very new to programming, so I may have made some mistakes with what I have said, apologies in advance.

原来有一个

turns out there was a really simple way to do it by adding one line to the setup.py file

推荐答案

您的安装程序仅将python脚本复制到指定目录.

Your installer only copies the python script to a specified directory.

要运行python脚本,您需要先安装python.

In order to run a python script you need to have python installed.

您可以使用PyInstaller之类的工具将脚本(.py文件)转换为可执行文件(在Windows上为.exe). PyInstaller的工作方式是将python解释器和脚本都复制到一个文件中,以便您可以轻松地分发程序.

You can use a tool like PyInstaller to convert your script (.py file) into an executable (.exe on windows). The way that works is PyInstaller copies both the python interpretor and your script into a single file so that you can distribuite your program easily.

将脚本转换为可执行文件后,需要将其添加到路径中,以便操作系统知道在哪里可以找到它.完成此操作后,您可以从任何目录的命令行运行程序.

After you have converted your script into an executable, you need to add it to the path so that your operating system knows where to find it. After you do that, you can run your program from the command line from any directory.

在Linux上也可以使用相同的过程,但是您必须对可执行文件进行单独的分发,因为Windows可执行文件与Linux可执行文件不同.

The same process will also work on Linux, but you'd have to make separate distributions of the executable because windows executables are different from linux executables.

这篇关于是否可以创建可在任何地方运行的,可分发给Windows的Python程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆