注意:我刚刚开始使用 ubuntu 和 python,在 Windows 下使用其他语言有丰富的编码经验。
因此,我想安装这个包:
https://github.com/google/protobuf/tree/master/python
我尝试通过以下方式安装它:我尝试下载此存储库中的代码,然后安装它。因此,我首先使用 SVN 下载了代码:
svn checkout https://github.com/google/protobuf/trunk/python
然后我尝试使用以下方法安装它:
sudo python setup.py install
输出结果如下:
running install
running bdist_egg
running egg_info
creating protobuf.egg-info
writing requirements to protobuf.egg-info/requires.txt
writing protobuf.egg-info/PKG-INFO
writing namespace_packages to protobuf.egg-info/namespace_packages.txt
writing top-level names to protobuf.egg-info/top_level.txt
writing dependency_links to protobuf.egg-info/dependency_links.txt
writing manifest file 'protobuf.egg-info/SOURCES.txt'
reading manifest file 'protobuf.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no previously-included files found matching 'google/protobuf/internal/*_pb2.py'
warning: no previously-included files found matching 'google/protobuf/internal/*.proto'
warning: no previously-included files matching '*_test.py' found under directory 'google'
warning: no previously-included files matching '*_test.proto' found under directory 'google'
warning: no previously-included files matching 'unittest*_pb2.py' found under directory 'google'
warning: no previously-included files matching '*.dll' found anywhere in distribution
warning: no previously-included files matching '*.pyc' found anywhere in distribution
warning: no previously-included files matching '*.pyo' found anywhere in distribution
warning: no previously-included files matching '*.so' found anywhere in distribution
writing manifest file 'protobuf.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
Generating google/protobuf/descriptor_pb2.py...
Can't find required file: ../src/google/protobuf/descriptor.proto
手动检查我的文件夹和原始 github 确认“descriptor.proto”确实不存在。我确实找到了 setup.py 的第 128-129 行:
# Generate necessary .proto file if it doesn't exist.
generate_proto("../src/google/protobuf/descriptor.proto")
因此,我认为这个文件应该自动创建,但由于某种原因并没有自动创建。有人知道为什么吗?
编辑:
我也尝试过使用:
sudo python setup.py build
这给出了类似的结果:
running build
running build_py
Generating google/protobuf/descriptor_pb2.py...
Can't find required file: ../src/google/protobuf/descriptor.proto
编辑 2:我现在提取了整个存储库并重新运行
sudo python setup.py build
输出结果如下:
running build
running build_py
Generating google/protobuf/descriptor_pb2.py...
google/protobuf/descriptor.proto:381:3: Expected "required", "optional", or "repeated".
google/protobuf/descriptor.proto:381:12: Expected field name.
google/protobuf/descriptor.proto:439:3: Expected "required", "optional", or "repeated".
google/protobuf/descriptor.proto:439:12: Expected field name.
google/protobuf/descriptor.proto:537:3: Expected "required", "optional", or "repeated".
google/protobuf/descriptor.proto:537:12: Expected field name.
descriptor.proto文件中对应的行是:
第381行:
reserved 38;
第 439 行:
reserved 8; // javalite_serializable
第 537 行:
reserved 4; // removed jtype
这也是唯一一次使用“保留 [数字]”语法的情况,所以我认为这种语法可能是导致问题的原因。有人对此有什么想法吗?
答案1
所以我从别人那里得到了答案,这最终对我有用:
首先下载所有代码(不仅仅是我最初下载的 Python 部分):
$ git clone https://github.com/google/protobuf.git
然后安装一些依赖项并编译:
$ cd protobuf/
$ apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev git autoconf automake libtool curl make g++ unzip
然后按照src/README.md:
$ ./autogen.sh
$ ./configure
$ make
$ make check
$ sudo make install
然后安装python部分,如python/README.md中所述
$ cd python
$ python setup.py build
$ python setup.py test
$ python setup.py install
答案2
问题是你只提取了部分存储库 ( protobuf/python
),但安装程序正在另一个目录中寻找文件 ( protobuf/src/google/protobuf/descriptor.proto
)
因此只需拉入完整的存储库:
$ git clone https://github.com/google/protobuf.git
然后:
$ cd protobuf/python
并按照 中的安装说明进行操作README.md
。