首页 > 解决方案 > 如何在没有 pyinstaller 的情况下使用“./”创建 python 可执行文件

问题描述

我需要将我的程序发送给将使用 makefile 编译它的人,并且没有 pyinstaller 或使用 pip3 安装任何东西。

它需要在linux上工作。

可能吗 ?(我只能找到关于 pyinstaller 和 pyexe 的答案)。

标签: pythonlinuxexecutable

解决方案


如果要file.py使用命令生成可执行文件./file.py,首先必须将 shebang 添加为文件中的第一行: #!/usr/bin/env python3对于 python 3.x 或者#!/usr/bin/env python2如果您仍在使用 python 2.7

下一步是更改文件的权限以使其可执行。

你可以通过输入chmod 744 file.py提示来做到这一点

$ nano file.py
  GNU nano 2.0.6                             File: file.py                  

#!/usr/bin/env python3

name = input("Name: ")

print('Hello, {}'.format(name))
$ ./file.py
-bash: ./file.py: Permission denied
$ chmod 744 file.py
$ ./file.py
Name: Osa
Hello, Osa

推荐阅读