PyInstaller:.exe运行,在尝试动态加载模块时抛出异常

时间:2018-03-01 22:26:17

标签: python-3.x executable

我用phonenumbers编写了一个Python程序。在安装了Python 3.6.4的Windows系统上,它按预期工作,但我想将其重新分发为独立的Windows可执行文件,因此我在其上运行PyInstaller以获取Windows可执行文件。

但是,我遇到了两个障碍:首先,我是Python的初学者,Python环境的细节对我来说仍然是一个障碍。

其次,.EXE程序抛出异常试图动态加载模块(国家数据解析数字,FWIW),我还没有设法弄清楚如何将动态加载的模块合并到独立的可执行文件中。 (失败是在“try”子句中,所以我花了一段时间来找到它。)我自己的系统拥有我需要预先加载所有需要的动态加载模块的所有内存,所以我想我喜欢这样做。

解决方案,建议,提示,想法?一个完整的例子?也许我在论坛上已经没有理解答案,但那是因为我是初学者,所以我很欣赏一些清晰度。

____

感谢您的留言。回答@ adnan-y和@apogalacticon:我做了一个简单的测试用例。以下是程序源,运行脚本的输出,程序上运行的pyinstaller(没有先前的构建目录或文件),其警告文件,运行可执行文件的结果以及Python脚本正确的文件的位置加载并正确使用样本输入。

源程序Parse-failure.py

import phonenumbers
Number = "+17034820623"
print("The unprocessed number is <%s>" % (Number))
Parsed_Number = phonenumbers.parse(Number)
print("The parsed number is <%s>" % (Parsed_Number))

运行Parse-failure.py

的输出
$ Parse-failure.py
The unprocessed number is <+17034820623>
The parsed number is <Country Code: 1 National Number: 7034820623>

Pyinstaller运行

$ PyInstaller --onefile Parse-failure.py
94 INFO: PyInstaller: 3.4.dev0
94 INFO: Python: 3.6.4
94 INFO: Platform: Windows-7-6.1.7601-SP1
95 INFO: wrote N:\PPR\Parse-failure.spec
96 INFO: UPX is not available.
97 INFO: Extending PYTHONPATH with paths
['f:\\Dems\\PhoneCheck', 'f:\\Dems\\PhoneCheck']
98 INFO: checking Analysis
98 INFO: Building Analysis because out00-Analysis.toc is non existent
98 INFO: Initializing module dependency graph...
99 INFO: Initializing module graph hooks...
101 INFO: Analyzing base_library.zip ...
5155 INFO: running Analysis out00-Analysis.toc
5157 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\python36\python.exe
6356 INFO: Caching module hooks...
6363 INFO: Analyzing N:\PPR\Parse-failure.py
6738 INFO: Loading module hooks...
6739 INFO: Loading module hook "hook-encodings.py"...
6843 INFO: Loading module hook "hook-pydoc.py"...
6845 INFO: Loading module hook "hook-xml.py"...
7238 INFO: Looking for ctypes DLLs
7238 INFO: Analyzing run-time hooks ...
7247 INFO: Looking for dynamic libraries
7372 INFO: Looking for eggs
7372 INFO: Using Python library d:\python36\python36.dll
7372 INFO: Found binding redirects:
[]
7375 INFO: Warnings written to N:\PPR\build\Parse-failure\warnParse-failure.txt
7461 INFO: Graph cross-reference written to N:\PPR\build\Parse-failure\xref-Parse-failure.html
7471 INFO: checking PYZ
7471 INFO: Building PYZ because out00-PYZ.toc is non existent
7471 INFO: Building PYZ (ZlibArchive) N:\PPR\build\Parse-failure\out00-PYZ.pyz
8292 INFO: Building PYZ (ZlibArchive) N:\PPR\build\Parse-failure\out00-PYZ.pyz completed successfully.
8302 INFO: checking PKG
8302 INFO: Building PKG because out00-PKG.toc is non existent
8302 INFO: Building PKG (CArchive) out00-PKG.pkg
10218 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
10222 INFO: Bootloader d:\python36\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
10222 INFO: checking EXE
10222 INFO: Building EXE because out00-EXE.toc is non existent
10222 INFO: Building EXE from out00-EXE.toc
10222 INFO: Appending archive to EXE N:\PPR\dist\Parse-failure.exe
10228 INFO: Building EXE from out00-EXE.toc completed successfully.

警告文件build \ PCS \ warnParse-failure.txt

missing module named resource - imported by posix, n:\PPR\Parse-failure.py
missing module named posix - imported by os, n:\PPR\Parse-failure.py
missing module named _posixsubprocess - imported by subprocess, n:\PPR\Parse-failure.py
missing module named org - imported by pickle, n:\PPR\Parse-failure.py
missing module named readline - imported by cmd, code, pdb, n:\PPR\Parse-failure.py
excluded module named _frozen_importlib - imported by importlib, importlib.abc, n:\PPR\Parse-failure.py
missing module named _frozen_importlib_external - imported by importlib._bootstrap, importlib, importlib.abc, n:\PPR\Parse-failure.py
missing module named _winreg - imported by platform, n:\PPR\Parse-failure.py
missing module named _scproxy - imported by urllib.request
missing module named java - imported by platform, n:\PPR\Parse-failure.py
missing module named 'java.lang' - imported by platform, n:\PPR\Parse-failure.py, xml.sax._exceptions
missing module named vms_lib - imported by platform, n:\PPR\Parse-failure.py
missing module named termios - imported by tty, n:\PPR\Parse-failure.py, getpass
missing module named grp - imported by shutil, tarfile, n:\PPR\Parse-failure.py
missing module named pwd - imported by posixpath, shutil, tarfile, http.server, webbrowser, n:\PPR\Parse-failure.py, netrc, getpass
missing module named _dummy_threading - imported by dummy_threading, n:\PPR\Parse-failure.py
missing module named 'org.python' - imported by copy, n:\PPR\Parse-failure.py, xml.sax

运行可执行文件

$ dist\Parse-failure.exe
[1364] Failed to execute script Parse-failure
The unprocessed number is <+17034820623>
Traceback (most recent call last):
  File "Parse-failure.py", line 7, in <module>
    Parsed_Number = phonenumbers.parse(Number)
  File "site-packages\phonenumbers\phonenumberutil.py", line 2835, in parse
  File "site-packages\phonenumbers\phonemetadata.py", line 294, in metadata_for_region_or_calling_code
  File "site-packages\phonenumbers\phonemetadata.py", line 267, in metadata_for_region
  File "site-packages\phonenumbers\data\__init__.py", line 23, in _load_region
ModuleNotFoundError: No module named 'phonenumbers.data.region_US'

...... region_US

的位置
D:\Python36\Lib\site-packages\phonenumbers\data\region_US.py

0 个答案:

没有答案