python打包成可執行檔

來源:互聯網
上載者:User

標籤:interval   gre   utf-8   try   word   字元   tools   int   技術   

1

最開始我直接把在Windows上打包的run.exe檔案上傳到Linux以為可以直接用了。但是./run後報錯。百度後知道,Windows上的程式不能在Linux上運行

Linux下檔案是否可執行可尾碼沒有關係,只和許可權有關係,靠的是檔案本身的許可權。想要執行就  chmod 755 filename 改變檔案許可權

windows和linux的二進位檔案不能相容,樓主檢查下吧,不能在linux下運行windows的程式.一定要在linux下運行,需要安裝wine

Linux預設支援ELF格式二進位檔案,Windows的PE格式運行不了的。

 

2 python用pyinstaller打包後,運行程式報錯"pkg_resources.DistributionNotFound"的解決辦法

pkg_resources.DistributionNotFound:the "APScheduler" distribution was not found....

這裡明明已經打包好了exe檔案,也沒有報錯。但是運行exe時候,卻彈出這個介面一閃而過。

之後再查閱了pyinstaller的官方文檔後,找到了解決辦法。

在目標檔案目錄下建立一個hook-ctypes.macholib.py檔案:

裡面的內容如下:

# -*- coding: utf-8 -*-

from PyInstaller.utils.hooks import copy_metadata

datas = copy_metadata(‘apscheduler‘)

然後打包的時候,多加一句--additional-hooks-dir=,如下所示:

pyinstaller -F yourfile.py --additional-hooks-dir=

這樣修改以後,打包出來的exe檔案就能夠正常使用了。

3 APScheduler: LookupError: No trigger by the name “interval” was found

環境

python: 2.6.6 

PyInstaller: 2.1 
APScheduler: 開始是3.0.1,後來是3.0.5

問題一

問題描述

以前在別的機器上開發的python程式(python2.7),在新的機器上運行時報錯

LookupError: No trigger by the name "interval" was found

程式碼

import os, timefrom datetime import datetimefrom apscheduler.schedulers.background import BackgroundSchedulerdef myjob():    print(‘myjob: %s‘ % datetime.now())    time.sleep(5)if __name__ == ‘__main__‘:    scheduler = BackgroundScheduler()    scheduler.add_job(myjob, ‘interval‘, seconds=1)    scheduler.start()    try:        while True:            time.sleep(5)    except (KeyboardInterrupt, SystemExit):        scheduler.shutdown()

原因

是由於低版本的setuptools導致

解決辦法

sudo pip install --upgrade setuptoolssudo pip install --ignore-installed apscheduler

然後再次運行上面的python代碼,問題解決。

問題二

問題描述

第一個問題解決後,在運行使用pyinstaller打包產生的可執行檔的時候報錯

 

Traceback (most recent call last):  File "<string>", line 11, in <module>  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 330, in add_job  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 782, in _create_trigger  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 766, in _create_plugin_instanceLookupError: No trigger by the name "interval" was found

 

原因

感覺好像是由於pyinstaller打包的時候使用了錯誤版本的APScheduler。(不確定)???

解決辦法

不要在add_job方法中使用“’interval’, seconds=1”做trigger,而是先建立一個IntervalTrigger對象,然後add_job的時候使用這個對象,即:

修改原來代碼中

    scheduler.add_job(myjob, ‘interval‘, seconds=1)

    trigger = IntervalTrigger(seconds=1)    scheduler.add_job(myjob, trigger)

完整代碼如下

def myjob():    print(‘myjob: %s‘ % datetime.now())    time.sleep(5)if __name__ == ‘__main__‘:    scheduler = BackgroundScheduler()    trigger = IntervalTrigger(seconds=1)    scheduler.add_job(myjob, trigger)    scheduler.start()    try:        while True:            time.sleep(5)    except (KeyboardInterrupt, SystemExit):        scheduler.shutdown()

然後用PyInstaller重新打包,此時再運行可執行檔的時候就不會報錯了。

重點:因為用到了IntervalTrigger,所以需要從包裡匯入,然後我找了這麼一個文章http://blog.csdn.net/mx472756841/article/details/51751616

用了這裡面的如下代碼

# 範例程式碼    from apscheduler.triggers.interval import IngervalTrigger    # 使用字串方式    scheduler.add_job(interval_tick,‘interval‘,seconds=4,minutes=2,                     start_date=datetime.now()+dt.timedelta(seconds=120),                     end_date=datetime.now()+dt.timedelta(seconds=360))    # 使用IntervalTrigger指定時間運行    trigger = IntervalTrigger(seconds=60,                               start_date=datetime.now()+dt.timedelta(seconds=60),                              end_date=datetime.now() + dt.timedelta(seconds=120))    scheduler.add_job(date_tick, trigger)

但是他這裡import是錯誤的。害我找了半天,後來用在在python內建的使用者圖形介面中import apschedluer 後用dir() 一步一步找到正確的名字,然後才運行通過的。

 4 打包成可執行檔後就需要串連本地的資料庫(XAMPP上的MySQL)

同時也是解決Navicat 報錯:1130-host ... is not allowed to connect to this MySql server,MySQL不允許從遠端存取的方法 (http://www.cnblogs.com/shyy/archive/2012/03/30/2453034.html).

https://jingyan.baidu.com/article/d169e186467a44436611d8b1.html

可以進入shell後操作(如果host為%號,那麼就是所有主機都可以登入,包括遠程主機.)

musql -uroot

select host,password,user,from user;

update user  set host = "%" where  host = "127.0.0.1"

相關文檔(http://blog.csdn.net/xiaomengh/article/details/48706149)

greenlet.h:8:20: 致命錯誤: Python.h:沒有那個檔案或目錄

解決方案是安裝python-dev,這是Python的標頭檔和靜態庫包:

sudo apt-get install python-dev 

但還是不行,TODO

 

TODO

 

python打包成可執行檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.