万万哇 发表于 2024-7-21 17:58:27

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

程序功能:
定时爬取一个网站的数据,处理后将其存在数据库中

环境:
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配置文件,内容如下:汉字部门需要根据实际环境配置

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")
https://img-blog.csdnimg.cn/direct/f9308f8c082b472b90ae066a78529ab3.jpeg

4.将instantclient_11_2文件夹路径配置在.spec文件的data项
https://img-blog.csdnimg.cn/direct/e6a72ee4259a48d490e5217613556b54.jpeg



部署到server 2012上报错:DLL load failed while importing pyexpat: 找不到指定的模块

https://img-blog.csdnimg.cn/direct/e51f93dc8e504d15aebd29ba847a8746.jpeg
报错原因:

报这个错是由于没有安装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项,如下图
https://img-blog.csdnimg.cn/direct/c8aea6fb75564626812f0fb4aa11ce3b.jpeg
 

打包成.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))+"\\"
    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"]

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 记录将python程序用pyinstaller打包成.exe并部署到服务器上碰到的问题