2014-01-22 41 views
0

Issue:我正在試圖安裝a Python3 port of Google Protocol Buffers。當我做一個python3 setup.py test我得到以下錯誤:Python相對輸入語法:`from。 import abc.xyz`

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解釋,並得到了同樣的錯誤:

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 

問:當是from . import abc.xyz有效的Python語法,如果有的話?我有興趣知道我下載的代碼是否內在地格式錯誤。

其他:我選擇GPB這Python3端口從一個答案上this SO question.這是達不到最新的關於GPB,但我希望它仍然起作用。讓我知道你是否更瞭解GPB的Python3端口。

+0

哪裏是'unittest_custom_options_pb2.py'文件從何而來? IT不在您鏈接到的[GitHub存儲庫](https://github.com/openx/python3-protobuf/tree/master/python/google/protobuf)中。 –

+0

啊,它似乎是從[此文件](https://github.com/openx/python3-protobuf/blob/master/src/google/protobuf/unittest_custom_options.proto)自動生成的。 –

+0

是的,這兩個違規文件中的每一行的第1行都說'#由協議緩衝區編譯器生成。不要編輯!'但是我按照你的建議編輯了它們,'setup.py test'運行正常。 – turbulencetoo

回答

0

您只能在import之後命名頂級對象或嵌套模塊。移動x名稱爲from條款:

from .x import y 

或您最初的問題:

from .google.protobuf import descriptor_pb2 

看樣子.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報告。

相關問題