74 python linux file-permissions
我刚刚安装了一个Linux系统(Kubuntu),并想知道是否有一个程序可以让python程序可执行linux.
Vin*_*ghe 135
只需将其放在脚本的第一行:
#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud)
使文件可执行
chmod +x myfile.py
Run Code Online (Sandbox Code Playgroud)
执行
./myfile.py
Run Code Online (Sandbox Code Playgroud)
您可以使用PyInstaller.它会生成一个build dist,因此您可以将其作为单个"二进制"文件执行.
http://pythonhosted.org/PyInstaller/#using-pyinstaller
Python 3还具有创建构建dist的本机选项:
https://docs.python.org/3/distutils/builtdist.html
将这些行放在代码的开头将告诉您的操作系统查找执行 python 脚本所需的二进制程序,即它是 python 解释器。
所以这取决于你的操作系统在哪里保存 python 解释器。因为我有 Ubuntu 作为操作系统,/usr/bin/python
所以它保留了 python 解释器,所以我必须在 python 脚本的开头写这一行;
#!/usr/bin/python
Run Code Online (Sandbox Code Playgroud)
完成并保存您的代码后
启动你的命令终端
确保脚本位于您当前的工作目录中
类型 chmod +x script_name.py
现在您可以通过单击脚本来启动脚本。将出现一个警告框;在警告框中按“运行”或“在终端中运行”;或者,在终端提示下,键入./script_name.py
小智 5
如果想使可执行hello.py
首先找到 python 在你的操作系统中的路径:which python
它通常位于“/usr/bin/python”文件夹下。
在第一行hello.py
应该添加:#!/usr/bin/python
然后通过linux命令chmod
人们应该让它可执行,例如:chmod +x hello.py
并执行./hello.py