我在 Anaconda 4.3 中使用 python 3.4 环境,并且正在尝试使用 py2exe 打包使用 selenium 的 python 脚本。该脚本作为 .py 文件完美运行,但 .exe 在尝试使用 import 语句从 selenium 模块导入 webdriver 时返回错误:
from selenium import webdriver
这是控制台中返回的回溯:
Traceback (most recent call last):
File "my_script.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Users\me\.conda\envs\py34\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Users\me\.conda\envs\py34\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 40, in <module>
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Users\me\.conda\envs\py34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 25, in <module>
from .webelement import WebElement
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Users\me\.conda\envs\py34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 40, in <module>
getAttribute_js = pkgutil.get_data(__package__, 'getAttribute.js').decode('utf8')
File "C:\Users\me\.conda\envs\py34\lib\pkgutil.py", line 611, in get_data
spec = importlib.util.find_spec(package)
File "C:\Users\me\.conda\envs\py34\lib\importlib\util.py", line 81, in find_spec
fullname = resolve_name(name, package) if name.startswith('.') else name
AttributeError: 'NoneType' object has no attribute 'startswith'
编辑将此表述为一个实际问题:当将文件作为 .py 脚本运行时,此导入语句可以正常工作。为什么只有在被 py2exe 打包后才会发生这种情况,并且在 py2exe 安装文件中是否缺少某种选项?