在linux上用什么来使python程序可执行
我刚刚安装了一个linux系统(Kubuntu),想知道是否有一个程序可以使python程序在linux上可执行。
I just installed a linux system (Kubuntu) and was wondering if there is a program to make python programs executable for linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
只需将其放在脚本的第一行:
使文件可执行
使用Execute with
Just put this in the first line of your script :
Make the file executable with
Execute with
如果您想在 Python 中获得独立的二进制应用程序,请尝试使用 py2exe 或 PyInstaller 等工具。
If you want to obtain a stand-alone binary application in Python try to use a tool like py2exe or PyInstaller.
您可以使用 PyInstaller。 它生成一个构建 dist,以便您可以将其作为单个“二进制”文件执行。
http://pythonhosted.org/PyInstaller/#using-pyinstaller
Python 3 具有本机还可以选择创建构建 dist:
https://docs.python.org/3.10/库/distutils.html
You can use PyInstaller. It generates a build dist so you can execute it as a single "binary" file.
http://pythonhosted.org/PyInstaller/#using-pyinstaller
Python 3 has the native option of create a build dist also:
https://docs.python.org/3.10/library/distutils.html
将这些行放在代码的开头将告诉您的操作系统查找执行 python 脚本所需的二进制程序,即它是 python 解释器。
所以这取决于你的操作系统保存 python 解释器的位置。 由于我使用 Ubuntu 作为操作系统,它将 python 解释器保留在
/usr/bin/python
中,因此我必须在 python 脚本的开头编写这一行;完成并保存代码后
启动命令终端
确保脚本位于当前工作目录中
输入
chmod +x script_name.py
现在您可以通过单击脚本来启动脚本。 将会出现一个警告框; 在警报框中按“运行”或“在终端中运行”; 或者,在终端提示符下,键入
./script_name.py
Putting these lines at the starting of the code will tell your operating systems to look up the binary program needed for the execution of the python script i.e it is the python interpreter.
So it depends on your operating system where it keeps the python interpreter. As I have Ubuntu as operating system it keeps the python interpreter in
/usr/bin/python
so I have to write this line at the starting of my python script;After completing and saving your code
Start your command terminal
Make sure the script lies in your present working directory
Type
chmod +x script_name.py
Now you can start the script by clicking the script. An alert box will appear; press "Run" or "Run in Terminal" in the alert box; or, at the terminal prompt, type
./script_name.py
如果想要生成可执行文件
hello.py
首先找到 python 在操作系统中的路径:
which python
它通常位于“/usr/bin/python”文件夹下。
在
hello.py
的第一行应该添加:#!/usr/bin/python
然后通过 linux 命令
chmod
应该只是使其可执行,例如:
chmod +x hello.py
并使用
./hello.py
执行If one want to make executable
hello.py
first find the path where python is in your os with :
which python
it usually resides under "/usr/bin/python" folder.
at the very first line of
hello.py
one should add :#!/usr/bin/python
then through linux command
chmod
one should just make it executable like :
chmod +x hello.py
and execute with
./hello.py
我执行以下操作:
这实际上将文件转换为 bash 可执行文件。 当您双击它时,它应该运行。 这适用于基于 Unix 的系统。
I do the following:
This essentially turns the file into a bash executable. When you double-click it, it should run. This works in Unix-based systems.
执行以下步骤:
/usr/ 创建指向
.py
的符号链接local/bin无论您是否有单个独立的 python 脚本,或者是否有主文件调用的多个依赖脚本,这些步骤都有效。
Do the following steps:
<script-name>.py
from/usr/local/bin
These steps works irrespective of whether you have single standalone python script or if you have multiple dependent script called by your main file.
因为我发现它有点模棱两可,至于你到底用“程序”指的是什么,我在这里给出一个答案,如何从 Linux 的命令行制作一个“包”程序可执行文件,如下所示之前没有回答过这个问题。
本质上你必须遵循官方的说明,但本质上,您必须执行以下步骤:
1.) 将您的程序重构为所示的结构 这里(您基本上可以在两种结构之间进行选择)
2.)假设您选择了“平面布局”并且您的项目名称是
awesome
(即假设您的源文件位于program/awesome
中),您在程序级别创建两个文件,setup.py
和setup.cfg
文件(即program
),内容如下:setup.py
:setup.cfg
:3.) 在您的
program/awesome 中
文件夹中,您创建一个__init__.py
文件,其中包含一个main
函数,然后您可以在其中启动“真正的”程序。即在您的
__init__.py
文件中至少放入以下代码以查看效果:4.) 使用例如
python setup.py install
来安装它5.) 从以下位置执行它命令行使用
awesome
,例如$>; 太棒了
希望这对任何人都有帮助 - Thinklex
as I find it a bit ambiguous, as to what exactly you refer to with a ''Program'', I present here an answer, how to make a ''package''-program executable from the command line in Linux, as this was not answered in this question before.
Essentially you have to follow the official instructions, but in essence, you have to do the following steps:
1.) Refactor your program into the structure presented here (you essentially have the choice between two structures)
2.) Assuming you chose the ''flat layout'' and your project name is
awesome
(i.e. assuming your source files lie inprogram/awesome
), you create two files,setup.py
andsetup.cfg
file, at your program level (i.e.program
), with the contents below:setup.py
:setup.cfg
:3.) In your
program/awesome
folder you create a__init__.py
file, with amain
function, where you can then start your ''real'' program.I.e. put into your
__init__.py
file at least the following code to see an effect:4.) Install it using e.g.
python setup.py install
5.) Execute it from the command line using
awesome
, e.g.$> awesome
Hope this helps anyone - Thinklex
另一种方法是创建别名。
例如在终端写入中:
写入
printhello
将运行 hello_world.py,但这只是暂时的。要使别名永久存在,您必须将它们添加到 bashrc 中,您可以通过在终端中编写以下内容来编辑它:
Another way to do it could be by creating an alias.
For example in terminal write:
Writing
printhello
will run hello_world.py, but this is only temporary.To make aliases permanent, you have to add them to bashrc, you can edit it by writing this in the terminal:
除了上面针对初学者的 leo pepes 答案之外 - 我只是使用 auto-py-to-exe 并在 Ubuntu 22.04/Python 3.10/VSC 中遵循以下步骤:
(auto-py-to-exe 是一个使用上面提到的 pyinstaller 的 web-gui)
pip3 install auto-py-to-exe
使用以下命令启动它:
auto-py-to-exe
在“脚本文件位置”下选择您的 .py 文件,
单击“转换为 exe”,
然后 您可以:
选择附加文件
仅转换一个文件(不包含文件夹)
将您的配置导出到 json 文件(设置)
以及更多关于 GUI 的内容
As addition to leo pepes answer above for beginners - I just used auto-py-to-exe and followed these steps in Ubuntu 22.04/Python 3.10/VSC:
(auto-py-to-exe is a web-gui using the pyinstaller mentioned above)
pip3 install auto-py-to-exe
start it with the command:
auto-py-to-exe
choose your .py file under "scriptfile location"
click on "convert to exe"
later on you can:
choose additional files
convert in only one file (without folder)
export your config to a json file(settings)
and much more over the GUI