码迷,mamicode.com
首页 > 编程语言 > 详细

python打包成可执行文件

时间:2017-11-01 01:03:58      阅读:744      评论:0      收藏:0      [点我收藏+]

标签: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, 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)

同时也是解决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打包成可执行文件

标签:interval   gre   utf-8   try   word   字符   tools   int   技术   

原文地址:http://www.cnblogs.com/wangboqi/p/7764514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!