dmPython 是 DM 提供的依据 Python DB API version 2.0 中 API 使用规定而开发的数据库访问接口。dmPython 实现这些 API,使 Python 应用程序能够对 DM 数据库进行访问。
dmPython 通过调用 DM DPI 接口完成 python 模块扩展。在其使用过程中,除 Python标准库以外,还需要 DPI 的运行环境。
应用服务器与数据库服务器分开规划,且应用服务器上不安装数据库,这种情况下安装dmPython
推荐使用随同数据库软件包下的相关文件以及驱动包,不推荐使用第三方渠道的下载包或其他数据库版本的目录文件以及驱动包
拷贝数据库安装目录的三类目录文件到应用服务器上
如:拷贝到应用服务器/data/dmsoft
下面。目录结构如下图:
## 查看应用用户的环境变量
[root@VM-0-17-centos dmPython]# cat ~/.bash_profile
## 修改新增用户的环境变量,注意目录最后面不要加/
[root@VM-0-17-centos dmPython]# vim ~/.bash_profile
export DM_HOME=/data/dmsoft
export LD_LIBRARY_PATH=$DM_HOME/bin:$LD_LIBRARY_PATH
export PATH=$PATH:$HOME/bin:$DM_HOME/bin
## 环境变量生效
[root@VM-0-17-centos dmPython]# source ~/.bash_profile
[root@VM-0-17-centos dmPython]# cd /data/dmsoft/dmPython
[root@VM-0-17-centos dmPython]# python setup.py install
编译驱动期间:警告信息可忽略,只需要关注错误的信息即可
[root@VM-0-17-centos dmPython]# pip list
[root@VM-0-17-centos dmPython]# vim py_conn.py
#!/usr/bin/python
#coding:utf-8
import dmPython
try:
conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236)
cursor = conn.cursor()
print('python: conn success!')
conn.close()
except (dmPython.Error, Exception) as err:
print(err)
[root@VM-0-17-centos dmPython]# python py_conn.py
python: conn success!
[root@dmdb python]# cd dmPython/
[root@dmdb dmPython]# python setup.py install
Traceback (most recent call last):
File "setup.py", line 97, in <module>
raise DistutilsSetupError(messageFormat % (userDmHome,DAMENG_VERSION))
distutils.errors.DistutilsSetupError: Dameng home (/opt/dmdbms) does not refer to an DM8.1 installation or dmdpi library missing.
报错原因:当前用户加载不到dmdbms的库文件,需添加环境变量
## 查看应用用户的环境变量
[root@VM-0-17-centos dmPython]# cat ~/.bash_profile
## 修改新增用户的环境变量,注意目录最后面不要加/
[root@VM-0-17-centos dmPython]# vim ~/.bash_profile
export DM_HOME=/data/dmsoft
export LD_LIBRARY_PATH=$DM_HOME/bin:$LD_LIBRARY_PATH
export PATH=$PATH:$HOME/bin:$DM_HOME/bin
## 环境变量生效
[root@VM-0-17-centos dmPython]# source ~/.bash_profile
[root@dmdb dmPython]# python setup.py install
running install
running bdist_egg
running egg_info
creating dmPython.egg-info
writing dmPython.egg-info/PKG-INFO
writing top-level names to dmPython.egg-info/top_level.txt
writing dependency_links to dmPython.egg-info/dependency_links.txt
writing manifest file 'dmPython.egg-info/SOURCES.txt'
reading manifest file 'dmPython.egg-info/SOURCES.txt'
writing manifest file 'dmPython.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
building 'dmPython' extension
creating build
creating build/temp.linux-x86_64-2.7
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DDM64 -I/home/dmdba/dmdbms/include -I/usr/include/python2.7 -c py_Dameng.c -o build/temp.linux-x86_64-2.7/py_Dameng.o -DBUILD_VERSION=2.3
In file included from py_Dameng.c:3:0:
py_Dameng.h:8:20: fatal error: Python.h: No such file or directory
#include
^
compilation terminated.
error: command 'gcc' failed with exit status 1
[root@dmdb dmPython]#
[root@dmdb dmPython]# rpm -qa |grep gcc
gcc-4.8.5-39.el7.x86_64
gcc-c++-4.8.5-39.el7.x86_64
gcc-gfortran-4.8.5-39.el7.x86_64
libgcc-4.8.5-39.el7.x86_64
[root@dmdb dmPython]# rpm -q gcc
gcc-4.8.5-39.el7.x86_64
[root@dmdb dmPython]#
报错原因:缺失python-devel包
[root@dmdb yum.repos.d]# yum install python-devel
In file included from Connection.c:8:0:
trc.h:45:8: warning: extra tokens at end of #endif directive [enabled by default]
#endif #_DMPATHON_TRC_H
^
Connection.c:46:47: error: ‘DSQL_ATTR_UKEY_NAME’ undeclared here (not in a function)
static udint4 gc_attr_ukey_name = DSQL_ATTR_UKEY_NAME;
^
Connection.c:47:47: error: ‘DSQL_ATTR_UKEY_PIN’ undeclared here (not in a function)
static udint4 gc_attr_ukey_pin = DSQL_ATTR_UKEY_PIN;
^
Connection.c: In function ‘Connection_Debug_inner’:
Connection.c:276:5: warning: pointer targets in passing argument 1 of ‘sprintf’ differ in signedness [-Wpointer-sign]
sprintf(sql_txt, "SP_SET_PARA_VALUE(1, 'SVR_LOG', %d)", debug_type);
In file included from Connection.c:5:0:
Connection.c: At top level:
py_Dameng.h:216:1: warning: ‘PyDecimal_Check’ defined but not used [-Wunused-function]
PyDecimal_Check(
^
Connection.c:45:16: warning: ‘gc_attr_ssl_pwd’ defined but not used [-Wunused-variable]
static udint4 gc_attr_ssl_pwd = DSQL_ATTR_SSL_PWD;
^
Connection.c:46:21: warning: ‘gc_attr_ukey_name’ defined but not used [-Wunused-variable]
static udint4 gc_attr_ukey_name = DSQL_ATTR_UKEY_NAME;
^
Connection.c:47:21: warning: ‘gc_attr_ukey_pin’ defined but not used [-Wunused-variable]
static udint4 gc_attr_ukey_pin = DSQL_ATTR_UKEY_PIN;
^
Connection.c:50:16: warning: ‘gc_attr_trace’ defined but not used [-Wunused-variable]
static udint4 gc_attr_trace = DSQL_ATTR_TRACE;
^
Connection.c:51:16: warning: ‘gc_attr_trace_file’ defined but not used [-Wunused-variable]
static udint4 gc_attr_trace_file = DSQL_ATTR_TRACEFILE;
^
error: command 'gcc' failed with exit status 1
处理方法:
# 换社区源码包编译通过python-126594-20201027.zip
creating build/lib.linux-x86_64-2.7
gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/py_Dameng.o build/temp.linux-x86_64-2.7/row.o build/temp.linux-x86_64-2.7/Cursor.o build/temp.linux-x86_64-2.7/Connection.o build/temp.linux-x86_64-2.7/Environment.o build/temp.linux-x86_64-2.7/Error.o build/temp.linux-x86_64-2.7/Buffer.o build/temp.linux-x86_64-2.7/exLob.o build/temp.linux-x86_64-2.7/exObject.o build/temp.linux-x86_64-2.7/tObject.o build/temp.linux-x86_64-2.7/var.o build/temp.linux-x86_64-2.7/vCursor.o build/temp.linux-x86_64-2.7/vDateTime.o build/temp.linux-x86_64-2.7/vInterval.o build/temp.linux-x86_64-2.7/vLob.o build/temp.linux-x86_64-2.7/vNumber.o build/temp.linux-x86_64-2.7/vObject.o build/temp.linux-x86_64-2.7/vString.o build/temp.linux-x86_64-2.7/vlong.o build/temp.linux-x86_64-2.7/exBfile.o build/temp.linux-x86_64-2.7/vBfile.o build/temp.linux-x86_64-2.7/trc.o -L/home/dmdba/dmdbms/bin -L/usr/lib64 -ldmdpi -lpython2.7 -o build/lib.linux-x86_64-2.7/dmPython.so
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-2.7/dmPython.so -> build/bdist.linux-x86_64/egg
creating stub loader for dmPython.so
byte-compiling build/bdist.linux-x86_64/egg/dmPython.py to dmPython.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying dmPython.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dmPython.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dmPython.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dmPython.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/dmPython-2.3-py2.7-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing dmPython-2.3-py2.7-linux-x86_64.egg
Copying dmPython-2.3-py2.7-linux-x86_64.egg to /usr/lib64/python2.7/site-packages
Adding dmPython 2.3 to easy-install.pth file
Installed /usr/lib64/python2.7/site-packages/dmPython-2.3-py2.7-linux-x86_64.egg
Processing dependencies for dmPython==2.3
Finished processing dependencies for dmPython==2.3
[root@dmdb dmPython]#
报错原因:未是被到dpi文件
解决办法:添加环境变量