我在 linux 上使用什么来使 python 程序可执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/304883/
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
What do I use on linux to make a python program executable
提问by
I just installed a linux system (Kubuntu) and was wondering if there is a program to make python programs executable for linux.
我刚刚安装了一个 linux 系统(Kubuntu),想知道是否有一个程序可以使 python 程序在 linux 上可执行。
采纳答案by Vincent Van Den Berghe
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
回答by Mohit Dabas
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.
将这些行放在代码的开头将告诉您的操作系统查找执行 python 脚本所需的二进制程序,即它是 python 解释器。
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;
所以这取决于你的操作系统在哪里保存 python 解释器。因为我有 Ubuntu 作为操作系统,/usr/bin/python
所以它保留了 python 解释器,所以我必须在 python 脚本的开头写这一行;
#!/usr/bin/python
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
启动你的命令终端
确保脚本位于您当前的工作目录中
类型
chmod +x script_name.py
现在您可以通过单击脚本来启动脚本。将出现一个警告框;在警告框中按“运行”或“在终端中运行”;或者,在终端提示下,键入
./script_name.py
回答by Mihai8
If you want to obtain a stand-alone binary application in Python try to use a tool like py2exe or PyInstaller.
如果您想在 Python 中获得独立的二进制应用程序,请尝试使用 py2exe 或PyInstaller 之类的工具。
回答by Leo Pepe
You can use PyInstaller. It generates a build dist so you can execute it as a single "binary" file.
您可以使用 PyInstaller。它生成一个构建 dist,因此您可以将其作为单个“二进制”文件执行。
http://pythonhosted.org/PyInstaller/#using-pyinstaller
http://pythonhosted.org/PyInstaller/#using-pyinstaller
Python 3 has the native option of create a build dist also:
Python 3 还具有创建构建 dist 的本机选项:
回答by Coco
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:
写入printhello
将运行 hello_world.py,但这只是暂时的。要使别名永久,您必须将它们添加到 bashrc,您可以通过在终端中编写以下内容来编辑它:
gedit ~/.bashrc
回答by Nilesh K.
If one want to make executable hello.py
如果想要使可执行文件 hello.py
first find the path where python is in your os with : which python
首先找到python在你的操作系统中的路径: which python
it usually resides under "/usr/bin/python" folder.
它通常位于“/usr/bin/python”文件夹下。
at the very first line of hello.py
one should add : #!/usr/bin/python
在一个的第一行hello.py
应该添加:#!/usr/bin/python
then through linux command chmod
然后通过linux命令 chmod
one should just make it executable like : chmod +x hello.py
一个应该只是使它可执行,如: chmod +x hello.py
and execute with ./hello.py
并执行 ./hello.py
回答by dan_the_ham-man
I do the following:
我执行以下操作:
- put #! /usr/bin/env python3 at top of script
- chmod u+x file.py
- Change .py to .command in file name
- 放 #!脚本顶部的 /usr/bin/env python3
- chmod u+x 文件.py
- 将文件名中的 .py 更改为 .command
This essentially turns the file into a bash executable. When you double-click it, it should run. This works in Unix-based systems.
这实质上将文件转换为 bash 可执行文件。当您双击它时,它应该会运行。这适用于基于 Unix 的系统。