11

My goal is to build an executable using pyinstaller. The python script I am trying to build imports grpc. The following is an example that illustrates the problem called hello.py.

  import grpc
  if __name__ == '__main__':
     print "hello world"

I do pyinstaller hello.py and that produces the expected dist directory. Then I run it like ./dist/hello/hello and I get error ImportError: No module named grpc.

So then I installed grpc using pip install grpc. When I rebuild the artifact I now get error Import grpc:No module named gevent.socket.

Reading online indicated that the correct items to install were actually grpcio and grpcio-tools. So I tried pip uninstall grpc pip install grpcio and pip install grpcio-tools. Doing this and rebuilding the artifact gave me error ImportError: No module named pkg_resources. Trying to pip install pkg_resources gives error: Could not find a version that satisfies the requirement pkg_resources

Having all grpcio grpcio-tools and grpc install gives the same error: Import grpc:No module named gevent.socket

This seems like it should be a very simple task. I simply want to use pyinstaller to build an artifact that has a dependency on grpc, how do I do this?

Andrew Dawson
  • 145
  • 1
  • 2
  • 8
  • did you try `pip install gevent`? *No module named gevent.socket* suggests to me `gevent` is not installed? – James Schinner Jan 23 '18 at 04:56
  • yes I did try that. I then get error `Import grpc:No module named coros`. Then I try `pip install coros` and I get error `Could not find a version that satisfies the requirement coro` – Andrew Dawson Jan 23 '18 at 05:04

2 Answers2

31

I faced the same issue. I referred this document: gRPC

As per the documentation, first upgrade your pip to version 9 or higher.

Then use the following commands:

$ python -m pip install grpcio
$ python -m pip install grpcio-tools

It worked for me!

  • `pip --version` gives `pip 9.0.1`. I ran `python -m pip install grpcio` and `python -m install grpcio-tools` and then running the built artifact gives `ImportError: No module named pkg_resources` – Andrew Dawson Jan 23 '18 at 06:06
  • It seems the setuptools has some errors or has been deleted. Reinstalling setuptools might fix the issue. Here is the link: (https://pypi.python.org/pypi/setuptools/0.9.8#installation-instructions) – Shrinivas Deshmukh Jan 23 '18 at 07:43
  • You can run this command: wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python – Shrinivas Deshmukh Jan 23 '18 at 07:46
  • that link gave me a 404 so I used `wget https://bootstrap.pypa.io/ez_setup.py -O - | python` followed by `python -m pip install grpcio` `python -m pip install grpcio-tools` `pyinstaller hello.py` `./dist/hello/hello` and that still gives `ImportError: No module named pkg_resources` doing python -m pip install pkg_resources` still gives No matching distribution found for pkg_resources` – Andrew Dawson Jan 23 '18 at 18:24
  • This is the issue of setuptools. try reinstalling pip using get-pip.py. It will install setuptools if they are corrupted or deleted somehow. Here is how You can do this: { wget https://bootstrap.pypa.io/get-pip.py } { python get-pip.py } – Shrinivas Deshmukh Jan 24 '18 at 05:26
5

I am working on doing a PyInstaller/cx_freeze distributable of a python app using grpc.

Can you try adding --hidden-import=pkg_resources and see what happens?

This solved it for me

Dominic Bou-Samra
  • 14,799
  • 26
  • 100
  • 156