python打包成可执行文件

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

python打包成可执⾏⽂件
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重新打包,此时再运⾏可执⾏⽂件的时候就不会报错了。

⽤了这⾥⾯的如下代码
# ⽰例代码
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)
可以进⼊shell后操作(如果host为%号,那么就是所有主机都可以登录,包括远程主机.)
musql -uroot
select host,password,user,from user;
update user set host = "%" where host = "127.0.0.1"
5 greenlet.h:8:20: 致命错误: Python.h:没有那个⽂件或⽬录
解决⽅法是安装python-dev,这是Python的头⽂件和静态库包:
sudo apt-get install python-dev
但还是不⾏,TODO
TODO。

相关文档
最新文档