zkpython安装遇到的问题记录

直接通过 pip install zkpython 时报错,提示找不到zookeeper.h,在网上找到了解决方法

zkpython 依赖于 zookeeper c 客户端,在安装zkpython 之前需要先安装 zookeeper c 客户端

安装 zookeeper c 客户端

wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
tar xf zookeeper-3.4.14.tar.gz
yum install make gcc autoconf cppunit automake libtool
cd zookeeper-3.4.14/zookeeper-client/zookeeper-client-c

autoreconf -i -f # 或者autoconf也可以,这步不是必须的
./configure --without-cppunit --prefix=/usr/local/zookeeper-client-c-3.4.14
make && make install

安装zkpython

# 下载安装包
pip download zkpython
tar xf zkpython-0.4.2.tar.gz
cd zkpython-0.4.2

# 如果设置LD_LIBRARY_PATH不生效,则使用下面的LIBRARY_PATH变量,这里测试是可以的
export LD_LIBRARY_PATH=/usr/local/zookeeper-client-c-3.4.14/lib
export LIBRARY_PATH=/usr/local/zookeeper-client-c-3.4.14/lib
ldconfig

# 编辑setup.py文件,内容如下
zookeepermodule = Extension("zookeeper",
                            sources=["zookeeper.c"],
                            include_dirs=["/usr/include/c-client-src", "/usr/local/include/c-client-src",
                                    "/usr/include/zookeeper", "/usr/local/include/zookeeper",
                                    "/usr/local/zookeeper-client-c-3.4.14/include/zookeeper"],    # 这里是刚刚安装的zookeeper-client-c的安装位置
                            libraries=["zookeeper_mt"],    # 本来想尝试修改这里添加刚刚安装的lib库路径,发现没有生效,需要使用下面的方法设置环境变量
                            )

setup( name="zkpython",
       version = "0.4.2",
       description = "ZooKeeper Python bindings",
       ext_modules=[zookeepermodule] )


python setup.py build
python setup.py install

到此, zkpython安装完成

python -c "import zookeeper; print(zookeeper.__file__)" /home/v/.venvs/py27/lib/python2.7/site-packages/zookeeper.so

$ python
Python 2.7.9 (default, Sep  9 2019, 18:10:45)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zookeeper
>>> zookeeper.__file__
'/home/v/.venvs/py27/lib/python2.7/site-packages/zookeeper.so'

>>> help(zookeeper)
Help on module zookeeper:

NAME
    zookeeper

FILE
    # 这里可以看到加载的路径
    /home/v/.venvs/py27/lib/python2.7/site-packages/zookeeper.so


如果感觉不需要 /usr/local/zookeeper-client-c-3.4.14/, 那么卸载的方法如下

# 需要先下载zookeeper-client-c-3.4.14的源码文件,参考上面步骤
# 进入到源码目录后

cd zookeeper-3.4.14/zookeeper-client/zookeeper-client-c
# ./configure 参数最好是和之前一样,如果之前的源码目录没有删除,那么直接使用 make uninstall即可
./configure --without-cppunit --prefix=/usr/local/zookeeper-client-c-3.4.14

# 卸载,不是所有的开源软件都提供了make uninstall,如果没有那就再重新安装一遍,make install的时候,看看都复制了那些文件
make uninstall

你可能感兴趣的:(zkpython安装遇到的问题记录)