简体   繁体   English

使用 Nuitka 进行 Python 独立编译

[英]Python standalone compiling with Nuitka

I'm having trouble compiling a simple python script into a standalone executable file.我在将一个简单的 python 脚本编译成一个独立的可执行文件时遇到了问题。 I coded a CLI tool to deploy our front-end app easily but despite trying every combination of parameters I could give to Nuitka, I never manage to get a working standalone script.我编写了一个 CLI 工具来轻松部署我们的前端应用程序,但尽管尝试了我可以提供给 Nuitka 的所有参数组合,但我从未设法获得一个可以工作的独立脚本。

At first I was not managing to get the "Requests" module in, as it is the only one I'm using that is not included in Python by default.起初我没有设法获取“请求”模块,因为它是我使用的唯一一个默认情况下未包含在 Python 中的模块。 Now I feel like it IS included by I'm getting errors regarding one of its dependencies.现在我觉得它包含在我收到有关其依赖项之一的错误中。

./cmd.dist/cmd.exe
Traceback (most recent call last):
  File "/home/user/code/cli/cmd.dist/cmd.py", line 12, in <module>
    import requests
  File "/home/user/code/cli/cmd.dist/requests/__init__.py", line 58, in requests
  File "/home/user/code/cli/cmd.dist/requests/utils.py", line 26, in utils
  File "/home/user/code/cli/cmd.dist/requests/compat.py", line 42, in compat
ImportError: No module named packages.ordered_dict

To get an idea, my imports look like that :为了得到一个想法,我的进口看起来像这样:

from os.path import expanduser
from base64 import b64encode
from io import FileIO
from optparse import OptionParser
from json import dumps
from sys import stdout
from os import path
from os import makedirs
import subprocess
import requests

I'm open to any suggestion on what would be a good way to achieve what I'm trying to do, which is having a simple executable file in /usr/local/bin that's in the path and that can be easily installed on Unix systems, without having to install pip etc我愿意接受任何关于什么是实现我正在尝试做的事情的好方法的建议,即在 /usr/local/bin 中有一个简单的可执行文件,该文件位于路径中,并且可以轻松安装在 Unix 上系统,而无需安装 pip 等

Ok, thanks to @shuttle87, I managed to make everything work by creating a setup.py file with a scripts and entry_points attribute inside it.好的,感谢@shuttle87,我通过创建一个包含脚本和 entry_points 属性的 setup.py 文件设法使一切正常。 scripts being an array containing only a path to my sole python file and entry_points being an object looking like so :脚本是一个数组,只包含我唯一的 python 文件的路径,entry_points 是一个看起来像这样的对象:

  {
    'console_scripts': ['cmd=script:main'],
  },

cmd being the command that you'll be able to call from anywhere to call your script and script:main means that when you type "cmd" it will call the main function from script.py! cmd 是您可以从任何地方调用以调用脚本的命令,而 script:main 意味着当您键入“cmd”时,它将从 script.py 调用 main 函数!

Thank you very much :)非常感谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM