在linux上用什么来使python程序可执行

发布于 2024-07-08 20:42:54 字数 64 浏览 5 评论 0原文

我刚刚安装了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

剩一世无双 2024-07-15 20:42:55

只需将其放在脚本的第一行:

#!/usr/bin/env python

使文件可执行

chmod +x myfile.py

使用Execute with

./myfile.py

Just put this in the first line of your script :

#!/usr/bin/env python

Make the file executable with

chmod +x myfile.py

Execute with

./myfile.py
远山浅 2024-07-15 20:42:55

如果您想在 Python 中获得独立的二进制应用程序,请尝试使用 py2exe 或 PyInstaller 等工具。

If you want to obtain a stand-alone binary application in Python try to use a tool like py2exe or PyInstaller.

陌伤浅笑 2024-07-15 20:42:55

您可以使用 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

蓝眸 2024-07-15 20:42:55

将这些行放在代码的开头将告诉您的操作系统查找执行 python 脚本所需的二进制程序,即它是 python 解释器。

所以这取决于你的操作系统保存 python 解释器的位置。 由于我使用 Ubuntu 作为操作系统,它将 python 解释器保留在 /usr/bin/python 中,因此我必须在 python 脚本的开头编写这一行;

#!/usr/bin/python

完成并保存代码后

  1. 启动命令终端

  2. 确保脚本位于当前工作目录中

  3. 输入 chmod +x script_name.py

  4. 现在您可以通过单击脚本来启动脚本。 将会出现一个警告框; 在警报框中按“运行”或“在终端中运行”; 或者,在终端提示符下,键入 ./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;

#!/usr/bin/python

After completing and saving your code

  1. Start your command terminal

  2. Make sure the script lies in your present working directory

  3. Type chmod +x script_name.py

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

極樂鬼 2024-07-15 20:42:55

如果想要生成可执行文件 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

晒暮凉 2024-07-15 20:42:55

我执行以下操作:

  1. 输入#! /usr/bin/env python3 位于脚本顶部
  2. chmod u+x file.py
  3. 将文件名中的 .py 更改为 .command

这实际上将文件转换为 bash 可执行文件。 当您双击它时,它应该运行。 这适用于基于 Unix 的系统。

I do the following:

  1. put #! /usr/bin/env python3 at top of script
  2. chmod u+x file.py
  3. Change .py to .command in file name

This essentially turns the file into a bash executable. When you double-click it, it should run. This works in Unix-based systems.

独自唱情﹋歌 2024-07-15 20:42:55

执行以下步骤:

  1. 将其作为第一行添加到执行入口点 python 文件中
#!/usr/bin/python
  1. 将脚本修改为可执行文件
    chmod +x <script-name>.py
  1. /usr/ 创建指向 .py 的符号链接local/bin
ln -s <path-to-your-script> /usr/local/bin/<executable-name-you-want>

无论您是否有单个独立的 python 脚本,或者是否有主文件调用的多个依赖脚本,这些步骤都有效。

Do the following steps:

  1. Add this as first line in to your execution entry point python file
#!/usr/bin/python
  1. Modify script to executable
    chmod +x <script-name>.py
  1. Create a symbolic link to your <script-name>.py from /usr/local/bin
ln -s <path-to-your-script> /usr/local/bin/<executable-name-you-want>

These steps works irrespective of whether you have single standalone python script or if you have multiple dependent script called by your main file.

天煞孤星 2024-07-15 20:42:55

因为我发现它有点模棱两可,至于你到底用“程序”指的是什么,我在这里给出一个答案,如何从 Linux 的命令行制作一个“包”程序可执行文件,如下所示之前没有回答过这个问题。

本质上你必须遵循官方的说明,但本质上,您必须执行以下步骤:

1.) 将您的程序重构为所示的结构 这里(您基本上可以在两种结构之间进行选择)

2.)假设您选择了“平面布局”并且您的项目名称是awesome(即假设您的源文件位于 program/awesome 中),您在程序级别创建两个文件,setup.pysetup.cfg 文件(即 program),内容如下:

setup.py:

from setuptools import setup
setup()

setup.cfg:

[metadata]
name = awesome
version = 0.0.1
description = My awesome program is 'awesomer' than yours
author =Awesome Name
email = [email protected]

[options]
packages = find:
install_requires = 
    <YOUR-REQUIREMENTS-HERE-DELETE-IF-NONE>

[options.entry_points]
console_scripts =
    awesome = awesome:main

3.) 在您的 program/awesome 中 文件夹中,您创建一个 __init__.py 文件,其中包含一个 main 函数,然后您可以在其中启动“真正的”程序。
即在您的 __init__.py 文件中至少放入以下代码以查看效果:

def main():
    print("MY AWESOME PROGRAM WORKS!")

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 in program/awesome), you create two files, setup.py and setup.cfg file, at your program level (i.e. program), with the contents below:

setup.py:

from setuptools import setup
setup()

setup.cfg:

[metadata]
name = awesome
version = 0.0.1
description = My awesome program is 'awesomer' than yours
author =Awesome Name
email = [email protected]

[options]
packages = find:
install_requires = 
    <YOUR-REQUIREMENTS-HERE-DELETE-IF-NONE>

[options.entry_points]
console_scripts =
    awesome = awesome:main

3.) In your program/awesome folder you create a __init__.py file, with a main 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:

def main():
    print("MY AWESOME PROGRAM WORKS!")

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

新雨望断虹 2024-07-15 20:42:55

另一种方法是创建别名。
例如在终端写入中:

alias printhello='python /home/hello_world.py'

写入 printhello 将运行 hello_world.py,但这只是暂时的。
要使别名永久存在,您必须将它们添加到 bashrc 中,您可以通过在终端中编写以下内容来编辑它:

gedit ~/.bashrc

Another way to do it could be by creating an alias.
For example in terminal write:

alias printhello='python /home/hello_world.py'

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:

gedit ~/.bashrc
维持三分热 2024-07-15 20:42:55

除了上面针对初学者的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文