표시되는 이유는 무엇입니까?"가져올 수 없습니다 이름 descriptor_pb2"오류가 발생 할 때 사용하여 Google 프로토콜 Buffers?

StackOverflow https://stackoverflow.com/questions/1828379

  •  11-09-2019
  •  | 
  •  

문제

을 사용할 때 생성되는 파이썬 코드에서 우리의 protobuf 클래스 우리는 이 오류가 발생할 수 있습니다.

cannot import name descriptor_pb2

해당하는 C++코드를 생성 작품을 정밀하고,그래서 그것은 나타나는 문제가 없으로 우리의 실제 프로토니다.

이 오류가 발생한 경우도 가져와 우리의 클래스,이렇게:

import sys
sys.path.append('..\path\to\generated')
sys.path.append('..\contrib\protobuf\python')

from foobar_pb2 import FooBar

그것은 올바른 추가하는 시스템 경로?

나는에서 확인 protobuf\python\google\protobuf 디렉토리 descriptor_pb2.py 하지만 발견 descriptor.py -우리는 최신 버전을 사용하므로 나는 가정이 우리에 파일이 없다.

사람이 무엇을 알고의 솔루션입니까?

도움이 되었습니까?

해결책

나는 당신이 생성해야한다고 생각합니다 descriptor_pb2.py ~와 함께 protoc 당신 자신:

protoc descriptor.proto --python_out=gen/

gen/ 생성 된 파이썬 클래스가있는 폴더입니다.

그 후 다음은 잘 작동합니다.

sys.path.append('../gen')
from descriptor_pb2 import FileDescriptorSet

../gen/descriptor_pb2.py 존재해야합니다.

다른 팁

필자의 경우, protobuf가 올바르게 설치되지 않았기 때문에 Descriptor_PB2를 찾지 못했습니다. Protobuf의 파이썬 하위 디렉토리에서

python setup.py build
python setup.py test
python setup.py install (as root)

readme 파일에 지시 된대로 protobuf 런타임 라이브러리를 설치하십시오. Descriptor_PB2.py는 설치 프로세스의 일부로 Protoc (Protobuf 컴파일러)에 의해 생성되어야하므로 패키지에서 직접 소스를 사용할 수 없습니다.

Windows 10에서 Python 2.7을 사용합니다.

제 경우에는 Protoc-3.0.0-Beta-2-Win32를 다운로드했습니다 https://github.com/google/protobuf/releases 이진 프로토 파일을 SRC 폴더에 복사했습니다.

그 후 나는 명령을 실행했다 Python setup.py 빌드 Desctriptor_PB2가 생성되었습니다.

python setup.py 축

이 단계는 필수입으로 생성되는 일부의 소스 파일이 있습니다.

성 google/protobuf/descriptor_pb2.py...성 글/protobuf/compiler/plugin_pb2.py...성 글/protobuf/unittest_pb2.py...성 글/protobuf/unittest_custom_options_pb2.py...성 글/protobuf/unittest_import_pb2.py...성 글/protobuf/unittest_import_public_pb2.py...성 글/protobuf/unittest_mset_pb2.py...성 글/protobuf/unittest_no_generic_services_pb2.py...성 글/protobuf/internal/descriptor_pool_test1_pb2.py...성 글/protobuf/internal/descriptor_pool_test2_pb2.py...성 글/protobuf/internal/test_bad_identifiers_pb2.py...성 글/protobuf/internal/missing_enum_values_pb2.py...성 글/protobuf/internal/more_extensions_pb2.py...성 글/protobuf/internal/more_extensions_dynamic_pb2.py...성 글/protobuf/internal/more_messages_pb2.py...성 글/protobuf/internal/factory_test1_pb2.py...성 글/protobuf/internal/factory_test2_pb2.py...성 글/protobuf/pyext/python_pb2.py...bla...

정확하게'descriptor_pb2.py'

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top