Importing Protos In Python

56 views
Skip to first unread message

Patrick

unread,
Aug 13, 2010, 1:39:27 PM8/13/10
to Protocol Buffers
I apologize if this is a repost but I could not find any information
searching the forums. I am trying to import two simple protos into
python 2.6.

D:\proto\src\MyProtos\foo.proto
package foobar;

option java_package = "foobar";
option java_outer_classname = "Foo";

message Foo
{
optional string the_foo = 1;
}

D:\proto\src\MyProtos\bar.proto
package foobar;

option java_package = "foobar";
option java_outer_classname = "Bar";

import "MyProtos/foo.proto";

message Bar
{
optional Foo my_foo = 1;
optional string bar_string = 2;
}

I compile the protos in windows with the following command:
D:\proto>protoc.exe --proto_path=src/ --python_out=build/gen src/
MyProtos/foo.proto src/MyProtos/bar.proto

I then run the following python script:
import sys

sys.path.append("D:\\proto\\build\\gen\\")

import foo_pb2
import bar_pb2

and receive the following error:
Traceback (most recent call last):
File "D:\Code\python\lbmpymodule\lbmpymodule\Release\test.py", line
15, in <module>
import bar_pb2
File "D:\Code\python\lbmpymodule\lbmpymodule\Release\bar_pb2.py",
line 52, in <module>
import MyProtos.foo_pb2
ImportError: No module named MyProtos.foo_pb2

What do I need to do to properly import these two protos into python.
Thanks,
Patrick

Kenton Varda

unread,
Aug 17, 2010, 8:12:42 PM8/17/10
to Patrick, Protocol Buffers
On Fri, Aug 13, 2010 at 10:39 AM, Patrick <doherty...@gmail.com> wrote:
import foo_pb2
import bar_pb2

Shouldn't these be:

  import MyProtos.foo_pb2
  import MyProtos.bar_pb2

Patrick

unread,
Aug 23, 2010, 12:00:44 PM8/23/10
to Protocol Buffers
Kenton,
My problem was solved when I added __init__.py files into the
directories. Thank you for your help.
Patrick

On Aug 17, 7:12 pm, Kenton Varda <ken...@google.com> wrote:
Reply all
Reply to author
Forward
0 new messages