记录将python程序用pyinstaller打包成.exe并部署到服务器上碰到的问题 ...

打印 上一主题 下一主题

主题 506|帖子 506|积分 1518

程序功能:
  定时爬取一个网站的数据,处理后将其存在数据库中
  
  环境:
  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.3
  SQLAlchemy                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配置文件,内容如下:汉字部门需要根据实际环境配置

  1. orcl =
  2.   (DESCRIPTION =
  3.     (ADDRESS_LIST =
  4.       (ADDRESS = (PROTOCOL = TCP)(HOST = 数据库地址)(PORT = 端口号))
  5.     )
  6.     (CONNECT_DATA =
  7.       (SERVER = DEDICATED)
  8.       (SERVICE_NAME = 服务名)
  9.     )
  10.   )
复制代码
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等
  1. pip install -r requirements.txt
复制代码
换环境后依赖如下:
  1. altgraph                  0.17.4
  2. attrs                     23.2.0
  3. beautifulsoup4            4.12.3
  4. certifi                   2024.2.2
  5. cffi                      1.16.0
  6. charset-normalizer        3.3.2
  7. cryptography              42.0.3
  8. greenlet                  3.0.3
  9. h11                       0.14.0
  10. idna                      3.6
  11. numpy                     1.26.4
  12. oracledb                  2.0.1
  13. outcome                   1.3.0.post0
  14. packaging                 23.2
  15. pandas                    2.2.0
  16. pefile                    2023.2.7
  17. pip                       24.0
  18. pycparser                 2.21
  19. pyinstaller               6.4.0
  20. pyinstaller-hooks-contrib 2024.1
  21. PySocks                   1.7.1
  22. python-dateutil           2.8.2
  23. pytz                      2024.1
  24. pywin32-ctypes            0.2.2
  25. requests                  2.31.0
  26. selenium                  4.18.1
  27. setuptools                65.5.0
  28. six                       1.16.0
  29. sniffio                   1.3.0
  30. sortedcontainers          2.4.0
  31. soupsieve                 2.5
  32. SQLAlchemy                2.0.27
  33. trio                      0.24.0
  34. trio-websocket            0.11.1
  35. typing_extensions         4.9.0
  36. tzdata                    2024.1
  37. urllib3                   2.2.1
  38. wsproto                   1.2.0
复制代码


打包成.exe文件后,报错找不到配置文件

 办理方法:
  1. def load_config_file(file_path):
  2.     if file_path=="":
  3.         file_path=os.path.dirname(os.path.realpath(sys.argv[0]))+"\"
  4.     try:
  5.         with open(file_path+"content_config.json",'r',encoding='utf-8') as load_config:
  6.             config_dict = json.load(load_config)
  7.         print("读取配置文件路径:"+file_path+"content_config.json")
  8.     except FileNotFoundError:
  9.         print("配置文件路径不存在:"+file_path+"重新查找路径...")
  10.         try:
  11.             with open(file_path+"_internal\\content_config.json",'r',encoding='utf-8') as load_config:
  12.                 config_dict = json.load(load_config)
  13.         except:
  14.             print("加载配置文件异常")
  15.             print("Unexpected error:", traceback.format_exc())
  16.         print("重新读取配置文件路径:"+file_path+"_internal\\content_config.json")
  17.     except:
  18.         print("加载配置文件异常")
  19.         print("Unexpected error:", traceback.format_exc())
  20.     return config_dict["purpose"]
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万万哇

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表