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, time from datetime import datetime from apscheduler.schedulers.background import BackgroundScheduler def 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 setuptools
sudo 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_instance LookupError: 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)
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)
5 greenlet.h:8:20: 致命錯誤: Python.h:沒有那個文件或目錄
解決方法是安裝python-dev,這是Python的頭文件和靜態庫包:
sudo apt-get install python-dev
但還是不行,TODO
TODO