【问题标题】:How to generate python exe of a python script including other python scripts如何生成python脚本的python exe,包括其他python脚本
【发布时间】:2016-10-12 02:37:56
【问题描述】:

我正在尝试为 python 脚本“cnss_image_loader.py”生成一个 exe,该脚本在同一文件夹中导入其他 python 脚本(见下文),现在我尝试使用命令python setup.py py2exe(见输出如下)并遇到以下错误,不确定它们是否是真正的问题,但是在运行生成的 exe 时会引发如下所示的错误

问题:

1.不能用py2exe生成可执行文件吗?

2.谁能准确指出我缺少的内容并提供生成 exe 的指导?

3.有没有更好的方法来生成python可执行文件?

错误:-

C:\Dropbox\py2exe\dist>cnss_image_loader.exe
Traceback (most recent call last):
  File "cnss_image_loader.py", line 9, in <module>
  File "android_dl.pyc", line 2, in <module>
  File "pip\__init__.pyc", line 14, in <module>
  File "pip\utils\__init__.pyc", line 22, in <module>
  File "pip\compat\__init__.pyc", line 26, in <module>
ImportError: No module named ipaddr

目录结构:-

cnss_image_loader.py

from android_dl import *
from alpaca import *

setup.py

from distutils.core import setup
import py2exe 
setup(console=['cnss_image_loader.py'])

python setup.py py2exe

The following modules appear to be missing
['Carbon', 'Carbon.Files', 'ElementC14N', 'OpenSSL.SSL', 'Pyrex.Distutils.build_ext', '_frozen_importlib', '_imp', '_manylinux', '_posixsubprocess', '_scproxy', '_sysconfigdata', 'backports.ssl_match_hostname', 'builtins', 'certifi', 'charade.universaldetector', 'chardet', 'chardet.universaldetector', 'configparser', 'datrie', 'genshi.core', 'html', 'html.entities', 'html.parser', 'http', 'http.client', 'http.cookies', 'importlib.machinery', 'importlib.util', 'ipaddr', 'ipaddress', 'java', 'lxml', 'lxml.etree', 'lzma', 'ndg.httpsclient.ssl_peer_verification', 'ndg.httpsclient.subj_alt_name', 'ordereddict', 'packages.six.moves', 'packages.ssl_match_hostname.CertificateError', 'packages.ssl_match_hostname.match_hostname', 'packages.urllib3.util.Timeout', 'packages.urllib3.util.parse_url', 'pip._vendor.six.moves.urllib', 'pyasn1.codec.der', 'pyasn1.type', 'queue', 'redis', 'reprlib', 'serial', 'serial.tools.list_ports', 'serializer.serialize', 'simplejson', 'sitecustomize', 'socks', 'tree
builders.getTreeBuilder', 'treewalkers.getTreeWalker', 'trie.Trie', 'urllib.error', 'urllib.parse', 'urllib.request', 'urllib3', 'urllib3.packages.backports.makefile', 'usercustomize', 'xmlrpc.client']

*** binary dependencies ***
Your executable(s) also depend on these dlls which are not included,
you may or may not need to distribute them.

Make sure you have the license if you distribute any of them, and
make sure you don't distribute files belonging to the operating system.

   OLEAUT32.dll - C:\WINDOWS\system32\OLEAUT32.dll
   USER32.dll - C:\WINDOWS\system32\USER32.dll
   IMM32.dll - C:\WINDOWS\system32\IMM32.dll
   SHELL32.dll - C:\WINDOWS\system32\SHELL32.dll
   ole32.dll - C:\WINDOWS\system32\ole32.dll
   COMDLG32.dll - C:\WINDOWS\system32\COMDLG32.dll
   COMCTL32.dll - C:\WINDOWS\system32\COMCTL32.dll
   ADVAPI32.dll - C:\WINDOWS\system32\ADVAPI32.dll
   NETAPI32.dll - C:\WINDOWS\system32\NETAPI32.dll
   WS2_32.dll - C:\WINDOWS\system32\WS2_32.dll
   GDI32.dll - C:\WINDOWS\system32\GDI32.dll
   VERSION.dll - C:\WINDOWS\system32\VERSION.dll
   KERNEL32.dll - C:\WINDOWS\system32\KERNEL32.dll
   ntdll.dll - C:\WINDOWS\system32\ntdll.dll

【问题讨论】:

  • 顺便说一句,如果python脚本不包含其他python脚本,则此方法有效

标签: python py2exe


【解决方案1】:

我个人一直在使用的是 pyinstaller。您可以在 cmd 上使用以下命令为 python 安装它:

pip install pyinstaller

安装后可以通过以下方式使用:

python -m pyinstaller main.py

main.py 是你的 python 模块。您还可以使用--icon 选项为您的应用程序提供图标文件,如下所示

python -m pyinstaller --icon=hi.ico main.py

您还可以使用--onefile 选项创建独立应用程序,如下所示:

python -m pyinstaller --onefile main.py

您可以在他们的网站https://www.pyinstaller.org 或他们的手册https://pyinstaller.readthedocs.io/en/stable/ 上找到更多信息

【讨论】:

  • Kamil,如果您提供一个链接,可以获取有关 pyinstaller 和使用它的更多详细信息,那就太好了。也许是一篇博文、软件包文档页面等。
  • @DejiS 我建议阅读他们的手册 (pyinstaller.readthedocs.io/en/stable),因为它非常详细地描述了如何使用它。抱歉回复晚了。
  • 不用担心。很高兴您编辑了答案以提供指向其网站和文档的链接。这应该大大方便理解您的答案谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 2013-08-29
  • 2011-09-22
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多