简体   繁体   English

使用cx_Freeze使grpc python代码成为exe文件:没有名为“ pkg_resources”的模块

[英]make grpc python code to exe with cx_Freeze : No module named 'pkg_resources'

I'm making grpc server with python. 我正在用python制作grpc服务器。 My goal is to build an executable using cx_Freeze. 我的目标是使用cx_Freeze构建可执行文件。

After I make greeter_server.exe using cx_Freeze, when I run greeter_server.exe, I meet this error. 使用cx_Freeze创建greeter_server.exe之后,运行greeter_server.exe时遇到此错误。

Traceback (most recent call last):  
File “greeter_server.py", line 19, in <module>   
File “c:\users\User\anaconda3\envs\tensorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module     exec(bytecode, module.__dict__)   
File “site-packages\grpc\__init__.py", line 22, in <module>  
File “src\python\grpcio\grpc\_cython\cygrpc.pyx", line 18, in init grpc._cython.cygrpc 
ModuleNotFoundError: No module named ‘pkg_resources'

My setup.py is like this. 我的setup.py就是这样。

import sys
from cx_Freeze import setup, Executable

build_exe_options = {}

setup(
    name='py freeze test',
    version='0.0.1',
    description="greeter_server function test",
    author="ray",
    options={'build_exe':build_exe_options},
    executables=[Executable("greeter_server.py")],
)

I tried a lot of tries. 我做了很多尝试。

pip install grp pip install grpcio pip install grpcio-tools pip install grp pip install grpcio pip安装grpcio-tools

pip install --upgrade setuptools pip install --upgrade distribute pip install --upgrade setuptools pip安装--upgrade分发

But when I tried pip install —upgrade distribute I meet this error. 但是,当我尝试点安装-升级分发时,遇到此错误。

Collecting distribute
  Using cached https://files.pythonhosted.org/packages/5f/ad/1fde06877a8d7d5c9b60eff7de2d452f639916ae1d48f0b8f97bf97e570a/distribute-0.7.3.zip
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\User\AppData\Local\Temp\pip-install-ao7m4k8f\distribute\setuptools\__init__.py", line 2, in <module>
        from setuptools.extension import Extension, Library
      File "C:\Users\User\AppData\Local\Temp\pip-install-ao7m4k8f\distribute\setuptools\extension.py", line 5, in <module>
        from setuptools.dist import _get_unpatched
      File "C:\Users\User\AppData\Local\Temp\pip-install-ao7m4k8f\distribute\setuptools\dist.py", line 7, in <module>
        from setuptools.command.install import install
      File "C:\Users\User\AppData\Local\Temp\pip-install-ao7m4k8f\distribute\setuptools\command\__init__.py", line 8, in <module>
        from setuptools.command import install_scripts
      File "C:\Users\User\AppData\Local\Temp\pip-install-ao7m4k8f\distribute\setuptools\command\install_scripts.py", line 3, in <module>
        from pkg_resources import Distribution, PathMetadata, ensure_directory
      File "C:\Users\User\AppData\Local\Temp\pip-install-ao7m4k8f\distribute\pkg_resources.py", line 1518, in <module>
        register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
    AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\User\AppData\Local\Temp\pip-install-ao7m4k8f\distribute\

I'm using Anaconda3-5.2.0, Python 3.6.7. 我正在使用Anaconda3-5.2.0,Python 3.6.7。 (Because I have to use tensorflow) (因为我必须使用tensorflow)

Any help will be appreciated. 任何帮助将不胜感激。

After editing setup.py file like bellow, it works. 在像下面这样编辑setup.py文件后,它就可以工作了。

build_exe_options = {
    'packages':['grpc','pkg_resources', 'numpy', 'tensorflow'],
}

pkg_resources needs to be added to the packages list of the build_exe options: 需要将pkg_resources添加到build_exe选项的packages列表中:

build_exe_options = {'packages': ['pkg_resources']}

Remark: other entries might be necessary in the packages list in order that the setup script works (these entries are found in the OP's own answer but are missing in the setup script of the question). 备注:为了使设置脚本起作用, packages列表中可能还需要其他条目(这些条目可在OP自己的答案中找到,但在问题的设置脚本中未找到)。

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

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