1、 首先,打開自己要發(fā)布的工程 2、 點(diǎn)擊底部的【Terminal】打開終端,中輸入命令pip install pyinstaller后回車,進(jìn)行安裝 3、輸入命令 pyinstaller,回車顯示安裝成功 4、pyinstaller -F -w alien_invasion.py封裝 遇到報(bào)錯(cuò)信息(原報(bào)錯(cuò)找不到了,網(wǎng)上找了類似的): Traceback (most recent call last): File "C:\Users\user\Desktop\untitled1\venv\Scripts\pyinstaller-script.py", line 11, in <module> load_entry_point('PyInstaller==3.4', 'console_scripts', 'pyinstaller')() File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\__main__.py", line 111, in run run_build(pyi_config, spec_file, **vars(args)) File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs) File "C:\Users\user\Desktop\untitled1\venv\lib\site- packages\PyInstaller\building\build_main.py", line 838, in main build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build')) File "C:\Users\user\Desktop\untitled1\venv\lib\site- packages\PyInstaller\building\build_main.py", line 784, in build exec(text, spec_namespace) File "<string>", line 29, in <module> File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\building\api.py", line 424, in __init__ strip_binaries=self.strip, upx_binaries=self.upx, File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\building\api.py", line 196, in __init__ self.__postinit__() File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__ self.assemble() File "C:\Users\user\Desktop\untitled1\venv\lib\site-packages\PyInstaller\building\api.py", line 273, in assemble pylib_name = os.path.basename(bindepend.get_python_library_path()) File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\ntpath.py", line 214, in basename return split(p)[1] File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\ntpath.py", line 183, in split p = os.fspath(p) TypeError: expected str, bytes or os.PathLike object, not NoneType 解決方法: 在github這個(gè)頁面上https://github.com/Loran425/pyinstaller/tree/14b6e65642e4b07a4358bab278019a48dedf7460 下載所有文件,解壓后找到文件夾PyInstaller里的bindepend.py文件,然后copy替換 X:\xxx\venv\Lib\site-packages\PyInstaller\depend里的bindepend.py,其中X:\xxx為你自己電腦上的路徑,不同的個(gè)人電腦路徑可能不太一樣。 然后在pycharm Terminal 運(yùn)行命令:pyinstaller -F xx.py即可。xx為python文件名。 pyinstaller后面如果加上-F就是打包為一個(gè)exe文件(文件會比較大),如果不加就會有很多庫文件;加上-w就是打包為沒有cmd窗口的exe,不加運(yùn)行時(shí)就會出現(xiàn)cmd窗口。(加不加憑個(gè)人喜好) 在工程目錄下/dist文件夾中會有打包好的exe文件。 此時(shí)輸入命令 pyinstaller會報(bào)錯(cuò),但暫時(shí)沒發(fā)現(xiàn)影響封裝結(jié)果 5、打包后,運(yùn)行時(shí)閃退怎么辦? 問題出在pygame的字體設(shè)置代碼上,原來錯(cuò)誤的代碼如下:
因?yàn)榇虬蟮膃xe找不到字體資源,所以就出錯(cuò)了。 將代碼替換為:
這樣就能調(diào)用系統(tǒng)已有的字體,再次打包后運(yùn)行成功
注意事項(xiàng): 1、ship和alien圖片的路徑為相對路徑(為絕對路徑時(shí),改變文件路徑后找不到文件) 2、封裝后將images文件夾放到exe同一個(gè)目錄下,否則無法讀取數(shù)據(jù) |
|