Linux 如何在 Ubuntu 中的可执行程序中转换 python 程序 .py?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3957717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How transform a python program .py in an executable program in Ubuntu?
提问by xRobot
I have a simple python program and I want an executable version (for Ubuntu Linux) of this program to avoid running it in the terminal with python myprogram.py
.
我有一个简单的 python 程序,我想要这个程序的可执行版本(适用于 Ubuntu Linux)以避免在终端中使用python myprogram.py
.
How can I do that ?
我怎样才能做到这一点 ?
采纳答案by relet
There is no need to. You can mark the file as executable using
没有必要。您可以使用以下命令将文件标记为可执行文件
chmod +x filename
Make sure it has a shebang line in the first line:
确保它在第一行中有一个 shebang 行:
#!/usr/bin/env python
And your linux should be able to understand that this file must be interpreted with python. It can then be 'executed' as
而且你的linux应该能够理解这个文件必须用python来解释。然后它可以“执行”为
./myprogram.py
回答by leonm
At the top op your python program add:
在您的python程序的顶部添加:
#!/usr/bin/python
回答by ghickman
As various others have already pointed out you can add the shebang to the top of your file
正如其他人已经指出的那样,您可以将shebang添加到文件的顶部
#!/usr/bin/python
or #!/usr/bin/env python
#!/usr/bin/python
或者 #!/usr/bin/env python
and add execution permissions chmod +x program.py
并添加执行权限 chmod +x program.py
allowing you to run your module with ./program.py
允许你运行你的模块 ./program.py
Another option is to install it the pythonic way with setuptools. Create yourself a setup.py and put this in it:
另一种选择是使用 setuptools 以 pythonic 方式安装它。为自己创建一个 setup.py 并将其放入其中:
from setuptools import setup
setup(
name = 'Program',
version = '0.1',
description = 'An example of an installable program',
author = 'ghickman',
url = '',
license = 'MIT',
packages = ['program'],
entry_points = {'console_scripts': ['prog = program.program',],},
)
This assumes you've got a package called program and within that, a file called program.py with a method called main(). To install this way run setup.py like this
这假设您有一个名为 program 的包,其中有一个名为 program.py 的文件,其中包含一个名为 main() 的方法。要以这种方式安装,请像这样运行 setup.py
python setup.py install
python setup.py install
This will install it to your platforms site-packages directory and create a console script called prog. You can then run prog
from your terminal.
这会将它安装到您的平台 site-packages 目录并创建一个名为 prog 的控制台脚本。然后,您可以prog
从终端运行。
A good resource for more information on setup.py is this site: http://mxm-mad-science.blogspot.com/2008/02/python-eggs-simple-introduction.html
有关 setup.py 的更多信息的一个很好的资源是这个站点:http: //mxm-mad-science.blogspot.com/2008/02/python-eggs-simple-introduction.html
回答by furkantokac
I know the easiest, exact and the best solution. I had the same problem like you but now, I can run my Python/Tkinter(GUI) program with its icon.
我知道最简单、准确和最好的解决方案。我和你有同样的问题,但现在,我可以用它的图标运行我的 Python/Tkinter(GUI) 程序。
As we create .bat files on Windows, we can also create equivalent the .bat files easily in Linux too. Thanks to this file, that, we can start our programs without terminal even if it needs to get command on terminal to start (like Python programs) with double click to its icon (really .png icon :) ) or we can write commands to facilitate our works. So, how is this going to happen ?
正如我们在 Windows 上创建 .bat 文件一样,我们也可以在 Linux 中轻松创建等效的 .bat 文件。多亏了这个文件,我们可以在没有终端的情况下启动我们的程序,即使它需要通过双击它的图标(实际上是 .png 图标 :) )在终端上获取命令来启动(如 Python 程序),或者我们可以将命令写入方便我们的工作。那么,这将如何发生?
For example, if we want to run our .py program, we just need to write this command to terminal :
例如,如果我们想运行我们的 .py 程序,我们只需要将这个命令写入终端:
python3 locationOfPyFile
python3 locationOfPyFile
So if we create a file that can automatically run this command, problem would be solved. In addition to that, you can have your own icon and even you don't have to open terminal !
所以如果我们创建一个可以自动运行这个命令的文件,问题就解决了。除此之外,您可以拥有自己的图标,甚至不必打开终端!
Check this article : Run Commands From It's Icon (Easiest Way)
检查这篇文章:从它的图标运行命令(最简单的方法)