anaconda3利用pyinstaller打包.py为.exe文件The ‘pathlib‘ package...问题
问题:anaconda3利用pyinstaller打包.py为.exe文件报如下错误:
(base) D:\>pyinstaller -c -F .\test.py
The 'pathlib' package is an obsolete backport of a standard library package and is incompatible with PyInstaller. Please remove this package (located in D:\anaconda3\Lib\site-packages) usingconda remove
then try again.
找了一些解决方案要conda remove pathlib,如下:
https://blog.csdn.net/m0_45295337/article/details/131034052
https://blog.csdn.net/m0_69311549/article/details/130145253
尴尬的是anaconda navigator也跟着remove了。
解决:找了个更好的解决方法是建个虚拟环境,在虚拟环境下就没有这个问题了(特别注意虚拟环境也得安装pyinstaller)。
另记录下anaconda3使用Pyinstaller打包.py为.exe文件时,通过--add-data添加打包资源文件后,.py代码中的路径要进行转换处理。
如打包成exe命令并添加file文件夹下所有文件: pyinstaller -c -F .\test.py --add-data=".\\file\\*;.\\file"
.py代码中的路径要进行转换处理才能使用打包的资源文件,路径转换方式如下:
#生成资源文件目录访问路径def resource_path(self, relative_path):if getattr(sys, 'frozen', False): #是否Bundle Resourcebase_path = sys._MEIPASSelse:base_path = os.path.abspath(".")return os.path.join(base_path, relative_path)def get_resource_path(self, relative_path):# 如果打包资源文件则要路径转换if hasattr(sys, '_MEIPASS'):return os.path.join(sys._MEIPASS, relative_path)else:return os.path.join(os.path.abspath("."), relative_path)