Python相对导入语法:`from。进口abc.xyz公司`

2024-04-20 07:27:59 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:我正在尝试安装a Python3 port of Google Protocol Buffers。当我执行python3 setup.py test操作时,会出现以下错误:

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

然后我在Python3和Python2解释器中尝试了相似的语法,得到了相同的错误:

^{pr2}$

问题:如果from . import abc.xyz什么时候是有效的Python语法?我有兴趣知道我下载的代码是否天生就是错误的。在

其他:我从this SO question.上的一个答案中选择了这个Python3 GPB端口,它不是GPB的最新版本,但我希望它仍然能正常工作。如果你对GPB的Python3端口更了解,请告诉我。在


Tags: of端口frompyimportport错误google
2条回答

此问题已修复:拉请求:https://github.com/openx/python3-protobuf/pull/7

只能在import之后命名顶层对象或嵌套模块。将x名称移到from子句:

from .x import y

或者你最初的问题:

^{pr2}$

似乎.proto file in question未正确编译为Python。快速扫描显示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");
}

你需要在项目中提交一个bug报告。在

相关问题 更多 >