安裝pyinstaller出錯的解決辦法及簡單使用

本文作者: 李瑞豪
修改時間: 2019-05-09 20:36:22
本文鏈接: https://lruihao.cn/posts/pyinstallererror.html
版權聲明: 本博客所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議。轉載請註明出處!

{% note %}
用過命令pip install pyinstaller安裝失敗,此包依賴於pywin32,安裝前需要先pip install pywin32,我安裝了還是出錯,稍微百度了一下也沒有看到解決辦法。
這裏通過手動下載安裝解決的,記錄一下。
{% endnote %}

下載

去官網下載pyinstaller安裝包:https://pypi.org/project/PyInstaller/#files

解壓

我這裏解壓到E:\應用\Python37\Lib\site-packages\PyInstaller-3.4

安裝

cmd也進入到上面的路徑下,然後執行Python setup.py install,等待安裝完畢
{% asset_img 1.png 安裝 %}
{% asset_img 2.png 完成 %}
{% asset_img 3.png pip list %}
{% asset_img 4.png 版本 %}

pyinstaller簡介

pyinstaller將Python腳本打包成可執行程序,使在沒有Python環境的機器上運行。

最新版是pyinstaller 3.4,可運行在Windows,Mac和Linux操作系統下。 但它不是跨編譯的,也就是說在Windows下用PyInstaller生成的exe只能運行在Windows下,在Linux下生成的只能運行在Linux下。

打包

打包的app裏並不包含任何源碼,但將腳本的.pyc文件打包了。

基本語法: pyinstaller options myscript.py

常用的可選參數如下:
--onefile 將結果打包成一個可執行文件
--onedir 將所有結果打包到一個文件夾中,該文件夾包括一個可執行文件和可執行文件執行時需要的依賴文件(默認)
--paths=DIR 設置導入路徑
--distpath=DIR 設置將打包的結果文件放置的路徑
--specpath=DIR 設置將spec文件放置的路徑
--windowed 使用windows子系統執行,不會打開命令行(只對windows有效)
--nowindowed 使用控制檯子系統執行(默認)(只對windows有效)
--icon=<FILE.ICO> 將file.ico添加爲可執行文件的資源(只對windows有效)

pyinstaller --paths="D:\" test.py

實例

比如,拿以前寫的一個刷csdn訪問量工具csdn.py(放在桌面上),代碼如下

__author__ = 'lruihao.cn'
import urllib.request
import re
import time
from bs4 import BeautifulSoup

opener = urllib.request.build_opener()
opener.addheaders = [('User-agent',
                      'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36')]

def get_article_url(page,name):
    endurl = "/"+name+"/article/details/........"
    print(name)
    p = re.compile(endurl)
    url = "http://blog.csdn.net/"+name+"/article/list/"+str(page)
    # 使用build_opener()是爲了讓python程序模仿瀏覽器進行訪問
    html = opener.open(url).read().decode('utf-8')
    allfinds = p.findall(html)
    return allfinds
    #print('allfinds',allfinds)

def start_do(allfinds):
    urlBase = "http://blog.csdn.net"  # 需要將網址合併的部分
    # 頁面中的網址有重複的,需要使用set進行去重複
    mypages = list(set(allfinds))
    for i in range(len(mypages)):
        mypages[i] = urlBase + mypages[i]
    print('要刷的網頁有:')
    for index, page in enumerate(mypages):
        print(str(index), page)
    # 設置每個網頁要刷的次數
    brushNum = 1

    # 所有的頁面都刷
    print('下面開始刷了哦:')
    for index, page in enumerate(mypages):
        for j in range(brushNum):
            try:
                pageContent = opener.open(page).read().decode('utf-8')
                # 使用BeautifulSoup解析每篇博客的標題
                soup = BeautifulSoup(pageContent)
                blogTitle = str(soup.title.string)
                blogTitle = blogTitle[0:blogTitle.find('-')]
                print(str(j), blogTitle)
            except urllib.error.HTTPError:
                print('urllib.error.HTTPError')
                time.sleep(3)  # 出現錯誤,停幾秒先
            except urllib.error.URLError:
                print('urllib.error.URLError')
                time.sleep(3)  # 出現錯誤,停幾秒先
                time.sleep(0.5)  # 正常停頓,以免服務器拒絕訪問

def main():
    name = input("輸入你的csdn用戶名:") 
    if name=="":
        name = "qq_39520417" # cheung99857 
    for page in range(1,5):
        print("************第"+str(page)+"頁*************")
        endurl = get_article_url(page,name)
        start_do(endurl)
    print("開始休息")
    time.sleep(40)

if __name__ == '__main__':
    while 1:
        main()

在cmd進入桌面路徑,輸入如下命令

pyinstaller --onefile --nowindowed csdn.py

{% asset_img 5.png csdn.exe生成成功 %}

另外推廣一下自己的微信公衆號,歡迎關注公衆號👇👇👇,後臺回覆關鍵詞csdn_visiter獲取源碼及exe可執行文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章