nanopb: ImportError: attempted relative import with no known parent package

Trying to build any .proto file on Windows using the batch file in release 0.4.1 fails with this “ImportError”. According to some other issue reports (#470 and others), this bug was already fixed - but maybe only for Linux? Or am I just doing this wrong?

D:\code\nanopb-0.4.1\generator>protoc.bat ..\tests\alltypes\alltypes.proto
Failed to build nanopb_pb2.py: protoc --python_out=D:\code\nanopb-0.4.1\generator\proto D:\code\nanopb-0.4.1\generator\proto\nanopb.proto -I=D:\code\nanopb-0.4.1\generator\proto
Failed to build nanopb_pb2.py: protoc --python_out=D:\code\nanopb-0.4.1\generator\proto D:\code\nanopb-0.4.1\generator\proto\nanopb.proto -I=D:\code\nanopb-0.4.1\generator\proto
Traceback (most recent call last):
  File "D:\code\nanopb-0.4.1\generator\nanopb_generator.py", line 42, in <module>
    from .proto import nanopb_pb2
ImportError: attempted relative import with no known parent package

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\code\nanopb-0.4.1\generator\\protoc", line 4, in <module>
    from nanopb_generator import invoke_protoc
  File "D:\code\nanopb-0.4.1\generator\nanopb_generator.py", line 63, in <module>
    import proto.nanopb_pb2 as nanopb_pb2
  File "D:\code\nanopb-0.4.1\generator\proto\__init__.py", line 33, in <module>
    invoke_protoc(argv=cmd)
  File "D:\code\nanopb-0.4.1\generator\proto\_utils.py", line 39, in invoke_protoc
    return subprocess.check_call(argv)
  File "C:\Users\bel\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 358, in check_call
    retcode = call(*popenargs, **kwargs)
  File "C:\Users\bel\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 339, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Users\bel\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "C:\Users\bel\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1207, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] System cannot find the specified file

```´

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 17 (6 by maintainers)

Most upvoted comments

If you are using the binary package for Windows, try it from the generator-bin folder.

For source package, try pip install protobuf grpcio-tools to get protoc installed.

As a solution: PYTHONPATH=…/…/lib/nanopb/generator protoc --nanopb_out=. tmp/message.prot

@wtford Yeah, that issue is #553.

I was able to fix the above problems, which were caused by the older python3-protobuf python module that was installed on my system and was taking precedence over the other protobuf library.

pip3 uninstall python3-protobuf

Ah, you’re also hitting #486, which is fixed in git master but not in 0.4.1. Also, you should be feeding the .proto file, not .c file.

Sorry about all the mess, with all of Python 2 vs. 3 and Windows 7 vs. 10 vs. Linux vs. Mac and different protoc versions it’s hard to test all combinations. I guess you can also try the two-step variant of ....protoc -osimple.pb simple.proto nanopb_generator.py simple.pb.