سؤال

Issue: I am trying to install a Python3 port of Google Protocol Buffers. When I do a python3 setup.py test I get the following error:

    File "/[*snip*]/python3-protobuf-master/python/google/protobuf/unittest_custom_options_pb2.py", line 13
    from . import google.protobuf.descriptor_pb2
                        ^
SyntaxError: invalid syntax

I then tried similar syntax in both my Python3 and Python2 interpreters and got the same error:

Python 3.2.3 (default, Jul 23 2012, 16:48:24)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from . import x.y
  File "<stdin>", line 1
    from . import x.y
                   ^
SyntaxError: invalid syntax

Question: When is from . import abc.xyz valid Python syntax, if at all? I am interested in knowing if this code I downloaded is inherently malformed.

Misc: I chose this Python3 port of GPB from an answer on this SO question. It is not up-to-date with regard to GPB, but I expected it to still function. Let me know if you know better about Python3 ports of GPB.

هل كانت مفيدة؟

المحلول

You can only name a top-level object or nested module after import. Move the x name to the from clause:

from .x import y

or for your original problem:

from .google.protobuf import descriptor_pb2

It appears the .proto file in question is not being compiled to Python correctly. A quick scan shows that this to be the case:

void Generator::PrintImports() const {
  for (int i = 0; i < file_->dependency_count(); ++i) {
    string module_name = ModuleName(file_->dependency(i)->name());
    printer_->Print("try:\n");
    printer_->Print("  from . import $module$\n", "module", module_name);
    printer_->Print("except ImportError:\n");
    printer_->Print("  import $module$\n", "module", module_name);
  }
  printer_->Print("\n");
}

You'll need to file a bug report with the project.

نصائح أخرى

This issue is fixed: pull request: https://github.com/openx/python3-protobuf/pull/7

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top