Lin*_* Ma 35 python protocol-buffers python-2.7
我正在遵循本指南(https://developers.google.com/protocol-buffers/docs/pythontutorial)并使用addressbook.proto的确切示例.
发布编译器生成的addressbook_pb2.py文件的内容.当我运行以下简单程序时,有错误说,找不到google.protobuf,任何想法如何解决这个问题?谢谢.
顺便说一句,在Mac OSX上使用Python 2.7.
from addressbook_pb2 import Person
p = Person()
p.email = "abc"
print p.email
Run Code Online (Sandbox Code Playgroud)
这是自动生成的文件addressbook_pb2.py,
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: addressbook.proto
import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor.FileDescriptor(
name='addressbook.proto',
package='tutorial',
syntax='proto2',
serialized_pb=_b('\n\x11\x61\x64\x64ressbook.proto\x12\x08tutorial\"\xda\x01\n\x06Person\x12\x0c\n\x04name\x18\x01 \x02(\t\x12\n\n\x02id\x18\x02 \x02(\x05\x12\r\n\x05\x65mail\x18\x03 \x01(\t\x12+\n\x05phone\x18\x04 \x03(\x0b\x32\x1c.tutorial.Person.PhoneNumber\x1aM\n\x0bPhoneNumber\x12\x0e\n\x06number\x18\x01 \x02(\t\x12.\n\x04type\x18\x02 \x01(\x0e\x32\x1a.tutorial.Person.PhoneType:\x04HOME\"+\n\tPhoneType\x12\n\n\x06MOBILE\x10\x00\x12\x08\n\x04HOME\x10\x01\x12\x08\n\x04WORK\x10\x02\"/\n\x0b\x41\x64\x64ressBook\x12 \n\x06person\x18\x01 \x03(\x0b\x32\x10.tutorial.Person')
)
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
_PERSON_PHONETYPE = _descriptor.EnumDescriptor(
name='PhoneType',
full_name='tutorial.Person.PhoneType',
filename=None,
file=DESCRIPTOR,
values=[
_descriptor.EnumValueDescriptor(
name='MOBILE', index=0, number=0,
options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='HOME', index=1, number=1,
options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='WORK', index=2, number=2,
options=None,
type=None),
],
containing_type=None,
options=None,
serialized_start=207,
serialized_end=250,
)
_sym_db.RegisterEnumDescriptor(_PERSON_PHONETYPE)
_PERSON_PHONENUMBER = _descriptor.Descriptor(
name='PhoneNumber',
full_name='tutorial.Person.PhoneNumber',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='number', full_name='tutorial.Person.PhoneNumber.number', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='type', full_name='tutorial.Person.PhoneNumber.type', index=1,
number=2, type=14, cpp_type=8, label=1,
has_default_value=True, default_value=1,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=128,
serialized_end=205,
)
_PERSON = _descriptor.Descriptor(
name='Person',
full_name='tutorial.Person',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='name', full_name='tutorial.Person.name', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='id', full_name='tutorial.Person.id', index=1,
number=2, type=5, cpp_type=1, label=2,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='email', full_name='tutorial.Person.email', index=2,
number=3, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='phone', full_name='tutorial.Person.phone', index=3,
number=4, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[_PERSON_PHONENUMBER, ],
enum_types=[
_PERSON_PHONETYPE,
],
options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=32,
serialized_end=250,
)
_ADDRESSBOOK = _descriptor.Descriptor(
name='AddressBook',
full_name='tutorial.AddressBook',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='person', full_name='tutorial.AddressBook.person', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=252,
serialized_end=299,
)
_PERSON_PHONENUMBER.fields_by_name['type'].enum_type = _PERSON_PHONETYPE
_PERSON_PHONENUMBER.containing_type = _PERSON
_PERSON.fields_by_name['phone'].message_type = _PERSON_PHONENUMBER
_PERSON_PHONETYPE.containing_type = _PERSON
_ADDRESSBOOK.fields_by_name['person'].message_type = _PERSON
DESCRIPTOR.message_types_by_name['Person'] = _PERSON
DESCRIPTOR.message_types_by_name['AddressBook'] = _ADDRESSBOOK
Person = _reflection.GeneratedProtocolMessageType('Person', (_message.Message,), dict(
PhoneNumber = _reflection.GeneratedProtocolMessageType('PhoneNumber', (_message.Message,), dict(
DESCRIPTOR = _PERSON_PHONENUMBER,
__module__ = 'addressbook_pb2'
# @@protoc_insertion_point(class_scope:tutorial.Person.PhoneNumber)
))
,
DESCRIPTOR = _PERSON,
__module__ = 'addressbook_pb2'
# @@protoc_insertion_point(class_scope:tutorial.Person)
))
_sym_db.RegisterMessage(Person)
_sym_db.RegisterMessage(Person.PhoneNumber)
AddressBook = _reflection.GeneratedProtocolMessageType('AddressBook', (_message.Message,), dict(
DESCRIPTOR = _ADDRESSBOOK,
__module__ = 'addressbook_pb2'
# @@protoc_insertion_point(class_scope:tutorial.AddressBook)
))
_sym_db.RegisterMessage(AddressBook)
# @@protoc_insertion_point(module_scope)
Run Code Online (Sandbox Code Playgroud)
编辑1,
试过pip install protobuf,遇到以下错误,
Requirement already satisfied (use --upgrade to upgrade): protobuf in /Users/foo/miniconda2/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.9 in /Users/foo/miniconda2/lib/python2.7/site-packages/six-1.10.0-py2.7.egg (from protobuf)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/foo/miniconda2/lib/python2.7/site-packages (from protobuf)
Run Code Online (Sandbox Code Playgroud)
这是python版本的输出,
python -V
Python 2.7.11 :: Continuum Analytics, Inc.
Run Code Online (Sandbox Code Playgroud)
**编辑2**
发布确切的错误消息,
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1531, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 938, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/foo/personal/featureExtraction/protobuf_test.py", line 1, in <module>
from addressbook_pb2 import Person
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_monkey_qt.py", line 71, in patched_import
return original_import(name, *args, **kwargs)
File "/Users/foo/personal/featureExtraction/addressbook_pb2.py", line 6, in <module>
from google.protobuf import descriptor as _descriptor
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_monkey_qt.py", line 71, in patched_import
return original_import(name, *args, **kwargs)
ImportError: No module named google.protobuf
Run Code Online (Sandbox Code Playgroud)
编辑3,
错误信息时import google,
编辑4,
输出which pip,
which pip
/Users/foo/miniconda2/bin/pip
Run Code Online (Sandbox Code Playgroud)
输出sys.executable,
/用户/富/蟒蛇/斌/蟒蛇
编辑5,
foo-mn1:featureExtraction foo$ sudo /Users/foo/miniconda2/bin/pip install protobuf
Password:
The directory '/Users/foo/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/foo/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied (use --upgrade to upgrade): protobuf in /Users/foo/miniconda2/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.9 in /Users/foo/miniconda2/lib/python2.7/site-packages/six-1.10.0-py2.7.egg (from protobuf)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/foo/miniconda2/lib/python2.7/site-packages (from protobuf)
foo-mn1:featureExtraction foo$ sudo /Users/foo/miniconda2/bin/pip install google
The directory '/Users/foo/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/foo/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied (use --upgrade to upgrade): google in /Users/foo/miniconda2/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4 in /Users/foo/miniconda2/lib/python2.7/site-packages (from google)
Run Code Online (Sandbox Code Playgroud)
Dav*_*mes 31
你应该运行:
pip install protobuf
Run Code Online (Sandbox Code Playgroud)
这将安装Google protobuf,之后您可以运行该Python脚本.
根据这个链接.
小智 31
有同样的问题,我通过使用:
conda install protobuf
Run Code Online (Sandbox Code Playgroud)
小智 14
我也有这个问题,研究了很久。在python 3+上似乎没有这样的问题。问题实际上出在 google.protobuf 上
pip uninstall protobuf
pip uninstall google
pip install google
pip install protobuf
pip install google-cloud
Run Code Online (Sandbox Code Playgroud)
__init__.py在“google”文件夹中创建一个。
cd /path/to/your/env/lib/python2.7/site-packages/google
touch __init__.py
Run Code Online (Sandbox Code Playgroud)
希望它会起作用。
vin*_*yal 10
这样做的原因大多是邪恶命令pip install google.我面临的问题google-cloud是同样的步骤也是如此,protobuf因为protobuf也安装在google > protobuf命名空间中.
如果你像我一样执行了这个命令那么你在正确的位置实际google包不是谷歌所拥有的,可以通过命令pip show google输出确认,其中应该有点如下
Name: google
Version: 1.9.3
Summary: Python bindings to the Google search engine.
Home-page: http://breakingcode.wordpress.com/
Author: Mario Vilas
Author-email: mvilas@gmail.com
License: UNKNOWN
Location: <Path where this package is installed>
Requires: beautifulsoup4
Run Code Online (Sandbox Code Playgroud)
因此,由于上面的google命名空间是为这个包保留的,巧合的是google-cloud也期望命名空间google > cloud,它会导致这两个包的命名空间冲突.
请参阅下面的截图命名空间google-protobufasgoogle > protobuf
解决方案: - google需要卸载非官方软件包,可以pip uninstall google在此之后使用,您可以google-cloud使用pip install google-cloud或protobuf使用重新安装pip install protobuf
FootNotes: - 假设您google错误地安装了非官方软件包,并且实际上并不需要它和google-cloud软件包一起使用.如果您需要非官方google和google-cloud以上解决方案将无法正常工作.
如果这解决了您的特定问题,请与我们联系.
小智 7
在Mac OS上 - 安装tensorflow 1.3 - 它将自动删除其他protobuf安装并安装protobuf 3.4.但是,这不起作用,也不会安装或降级到任何其他protobuf版本.
但是我找到了解决方案.不知道为什么会这样 - 但在Mac OS上,这解决了它.
pip install google
Run Code Online (Sandbox Code Playgroud)
当pip告诉你你已经拥有protobuf,但PyCharm(或其他)告诉你你没有它时,这意味着pip和PyCharm正在使用不同的Python解释器.这是一个非常常见的问题,特别是在Mac上,没有标准的Python包管理.
完全消除此类问题的最佳方法是使用virtualenv每个Python项目,该项目本质上是Python包和环境变量设置的目录,用于将项目的Python环境与其他所有内容隔离开来.
virtualenv像这样为你的项目创建一个:
cd project
virtualenv --distribute virtualenv -p /path/to/python/executable
Run Code Online (Sandbox Code Playgroud)
这将创建一个virtualenv在项目中调用的目录.(确保配置VCS(例如Git)忽略此目录.)
要在此安装软件包virtualenv,您需要激活环境变量设置:
. virtualenv/bin/activate
Run Code Online (Sandbox Code Playgroud)
通过运行验证pip将在其中使用正确的Python可执行文件.它应该告诉你使用的Python库路径,它应该在里面.virtualenvpip -Vvirtualenv
现在您可以像以前一样使用pip安装protobuf.
最后,您需要让PyCharm使用它virtualenv而不是系统库.在项目设置的某处,您可以为项目配置解释器,在其中选择Python可执行文件virtualenv.
安装protobuf:
pip install protobuf
Run Code Online (Sandbox Code Playgroud)
然后像这样导入它:
import google.protobuf
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73982 次 |
| 最近记录: |