我正在尝试冻结的Python脚本中使用TensorFlow。一切正常,但是当我尝试运行它时,出现此错误:
Traceback (most recent call last):
File "C:\Users\.conda\envs\lib\site-packages\cx_Freeze\initscripts__startup__.py", line 14, in run module.run()
File "C:\Users\.conda\envs\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run exec(code, m.dict)
File "config.py", line 21, in
File "E:\Projects\test.py", line 7, in from google.cloud import vision
ImportError: cannot import name 'vision'
我正在使用cx_Freeze。 Google库已成功添加到Setup.py
脚本中。 __init__.py
也出现在google目录中。
Setup.py
:
from cx_Freeze import setup, Executable
import sys
import os
base = None
os.environ['TCL_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\tcl\\tk8.6"
os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.getcwd(), "cacert.pem")
if sys.platform == 'win32':
# base = "Win32GUI"
base = 'Console'
if sys.platform == 'win64':
# base = "Win64GUI"
base = 'Console'
include = ['numpy.core._methods', 'numpy.lib.format', 'jinja2', 'jinja2.ext', 'asyncio', 'constants', 'jinja2.debug']
packages = ['asyncio', 'jinja2', 'jinja2.ext', 'flask', 'numpy', 'pandas', 'connect_db', 'pymysql', 'pymysql.cursors',
'multiprocessing', 'multiprocessing.pool','google']
excludes = []
options = {'build_exe': {'packages': packages, 'includes': include, 'include_files': includefiles, 'excludes': excludes}}
flaskapp = Executable(script="config.py",
targetName="test.exe",
icon='test.ico'
)
setup(
name="",
version="1.0",
author="Test",
description="Test",
options={
'build_exe': {
'include_files': includefiles,
'includes': include,
'packages': packages,
'excludes': excludes,
'build_exe': "build"
}
},
executables=[flaskapp]
)
提前谢谢
答案 0 :(得分:1)
根据GitHub存储库,google/cloud/__init__.py
包含以下代码:
try:
import pkg_resources
pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
cx_Freeze可能无法识别此动态导入。尝试在安装脚本的'pkg_resources'
列表中添加'pkgutil'
或'packages'
。
编辑:
显然,当__init__.py
与pip一起安装时,path_to_python_lib/site-packages/google/cloud/
目录中也缺少google-cloud-vision
文件。冻结之前,请尝试手动将一个空的__init__.py
文件添加到该目录。
答案 1 :(得分:1)
谢谢@jpeg,我终于找到了解决方法。
__init__.py
的{{1}}中也添加了/site-packages/google
/site-packages/google/cloud
编辑了DistributionNotFound: The 'google-api-core' distribution was not found and is required by the application
/site-packages/google/api_core/__init__.py
答案 2 :(得分:0)
我不得不如下修改A14提出的解决方案。
在导入后添加此行
from pkg_resources import py2_warn
对于错误:DistributionNotFound:未找到“ google-api-core”分发,并且由编辑/site-packages/google/api_core/init.py的应用程序所必需。
import pkg_resources
old_get_distribution = pkg_resources.get_distribution
from pkg_resources import parse_version
def mp_get_distribution(*args, **kwargs):
# print args
# print kwargs
try:
res = old_get_distribution(*args, **kwargs)
except:
class Mock(object):
pass
mock = Mock()
mock.parsed_version = parse_version("1.16.0") #add sensible value
mock.version = parse_version("1.16.0") #add sensible value
return mock
return res
pkg_resources.get_distribution = mp_get_distribution
from google import api_core