程序功能:
定时爬取一个网站的数据,处理后将其存在数据库中
环境:
python3.12.0
虚拟环境venv
pyinstaller6.3.0
服务器版本windows server 2012 r2
其他依赖版本如下:
beautifulsoup4 4.12.2
bs4 0.0.1
numpy 1.26.3
oracledb 2.0.1 #我用的python版本高,所以用oracledb库,因为cx_Oracle只适用于低版本的python
pandas 2.1.4
pip 23.3.2
requests 2.31.0
selenium 4.16.0
setuptools 69.0.3SQLAlchemy 2.0.25
程序运行报错:oracledb.exceptions.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found"
报错原因:
没安装oracle客户端
解决方法:
官网下载链接:Oracle Instant Client Downloads
1.下载与python版本相近的版本,将下载的压缩包解压到python项目工作目录下,生成instantclient_11_2文件夹
2.在instantclient_11_2目录下新建tnsnames.ora配置文件,内容如下:汉字部分需要根据实际情况配置
orcl = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 数据库地址)(PORT = 端口号)) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = 服务名) ) )
3.连接数据库之前设置instantclient_11_2路径:oracledb.init oracle client(lib dir=r".\instantclient 19 21")
4.将instantclient_11_2文件夹路径配置在.spec文件的data项
部署到server 2012上报错:DLL load failed while importing pyexpat: 找不到指定的模块
报错原因:
报这个错是因为没有安装MS visual C++,下载链接:https://www.microsoft.com/en-us/download/details.aspx?id=53587
参考资料:
Introducing the Universal CRT - C++ Team Blog (microsoft.com)
ImportError: DLL load failed while importing pyexpat-CSDN博客
python - ImportError: DLL load failed while importing pyexpat: 找不到指定的模块_Stack Overflow中文网
解决方法:
方法1:
安装MS visual C++(我没有成功,安装时windows server 2012报错缺少dll),所以只能手动将所有的dll文件都包含到python项目
方法2:
1.安装Microsoft Visual Studio,下载链接:Visual Studio: IDE and Code Editor for Software Developers and Teams
2.打开{安装目录}Microsoft Visual Studio\2022\Professional\Common7\IDE\Remote Debugger\x64\,搜索里面所有的.dll文件,复制到一个文件夹里(我的文件名为dll dict),将该文件夹放到python项目目录下,并配置.spec文件的binaries项,如下图
打包成.exe文件后 运行报错:numpy: Error importing numpy: you should not try to import numpy fromits source directory; please exit the numpy source tree, and relaunchyour python interpreter from there.
网络上的方法都试了,解决不了,只能换环境
将环境换为python3.11,重新创建虚拟环境,重新安装依赖,这里注意,安装依赖时不要使用如下命令安装,因为每个python对应不同版本的pandas、numpy、pyinstaller等
pip install -r requirements.txt
换环境后依赖如下:
altgraph 0.17.4 attrs 23.2.0 beautifulsoup4 4.12.3 certifi 2024.2.2 cffi 1.16.0 charset-normalizer 3.3.2 cryptography 42.0.3 greenlet 3.0.3 h11 0.14.0 idna 3.6 numpy 1.26.4 oracledb 2.0.1 outcome 1.3.0.post0 packaging 23.2 pandas 2.2.0 pefile 2023.2.7 pip 24.0 pycparser 2.21 pyinstaller 6.4.0 pyinstaller-hooks-contrib 2024.1 PySocks 1.7.1 python-dateutil 2.8.2 pytz 2024.1 pywin32-ctypes 0.2.2 requests 2.31.0 selenium 4.18.1 setuptools 65.5.0 six 1.16.0 sniffio 1.3.0 sortedcontainers 2.4.0 soupsieve 2.5 SQLAlchemy 2.0.27 trio 0.24.0 trio-websocket 0.11.1 typing_extensions 4.9.0 tzdata 2024.1 urllib3 2.2.1 wsproto 1.2.0
打包成.exe文件后,报错找不到配置文件
解决方法:
def load_config_file(file_path): if file_path=="": file_path=os.path.dirname(os.path.realpath(sys.argv[0]))+"\\" try: with open(file_path+"content_config.json",'r',encoding='utf-8') as load_config: config_dict = json.load(load_config) print("读取配置文件路径:"+file_path+"content_config.json") except FileNotFoundError: print("配置文件路径不存在:"+file_path+"重新查找路径...") try: with open(file_path+"_internal\\content_config.json",'r',encoding='utf-8') as load_config: config_dict = json.load(load_config) except: print("加载配置文件异常") print("Unexpected error:", traceback.format_exc()) print("重新读取配置文件路径:"+file_path+"_internal\\content_config.json") except: print("加载配置文件异常") print("Unexpected error:", traceback.format_exc()) return config_dict["purpose"]