python3使用pyinstaller打包apscheduler出的错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3使用pyinstaller打包apscheduler出的错相关的知识,希望对你有一定的参考价值。

  本来只是想用Python做一个定时任务小工具在服务器上运行,可是服务器在隔离区,各种禁止上外网,使用pip导出列表那种下载库的方法不管用,导致Python的各种库都下不到,官网离线下载又各种缺依赖,好气啊。后来查了一下,原来还有pyinstaller这种好东西,将需要的库和程序打包成一个可运行的程序,这正是我需要的。  

  为了测试pyinstaller,主要遇到了两个错误,一个是pkg_resources.DistributionNotFound,一个是ImportError: No module named。下面又开始说起,需要看主要解决办法的可以看大标题。

  先搞一个测试小程序吧,这个程序很简单,每5s打印一次a:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from apscheduler.schedulers.blocking import BlockingScheduler


def p():
    print("a")

if __name__ == __main__:
    scheduler = BlockingScheduler()
    scheduler.add_job(p, "cron",second=1/5)

    try:
        scheduler.start()
    except Exception as e:
        pass

使用pyinstaller打包成一个单独可运行的文件:

pyinstaller -F test1.py

pkg_resources.DistributionNotFound

可是出错了,提示找不到APScheduler,可我已经下载了啊,苦闷。

Traceback (most recent call last):
  File "test1.py", line 4, in <module>
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
  File "c:\users\fstmp\appdata\local\programs\python\python35-32\lib\site-packag
es\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
    exec(bytecode, module.__dict__)
  File "site-packages\apscheduler\__init__.py", line 2, in <module>
  File "site-packages\pkg_resources\__init__.py", line 535, in get_distribution
  File "site-packages\pkg_resources\__init__.py", line 415, in get_provider
  File "site-packages\pkg_resources\__init__.py", line 943, in require
  File "site-packages\pkg_resources\__init__.py", line 829, in resolve
pkg_resources.DistributionNotFound: The APScheduler distribution was not found
 and is required by the application
Failed to execute script test1

我查了一下,官方Issues给出了答案https://github.com/pyinstaller/pyinstaller/issues/1713

大概就是创建一个hook-ctypes.macholib.py,内容是:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from PyInstaller.utils.hooks import copy_metadata

datas = copy_metadata(apscheduler)

然后打包的时候添加--additional-hooks-dir=

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

可是,又出现了一个新问题ImportError: No module named

ImportError: No module named

Traceback (most recent call last):
  File "site-packages\apscheduler\schedulers\base.py", line 880, in _create_plug
in_instance
KeyError: cron

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test1.py", line 14, in <module>
  File "site-packages\apscheduler\schedulers\base.py", line 411, in add_job
  File "site-packages\apscheduler\schedulers\base.py", line 905, in _create_trig
ger
  File "site-packages\apscheduler\schedulers\base.py", line 883, in _create_plug
in_instance
  File "site-packages\pkg_resources\__init__.py", line 2229, in load
  File "site-packages\pkg_resources\__init__.py", line 2235, in resolve
ImportError: No module named apscheduler.triggers.cron
Failed to execute script test1

pkg_resources.DistributionNotFound问题好像解决了,但又来了一个新问题ImportError: No module named

看描述好像是cron哪里出了问题,把CronTrigger单独新建,修正后:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger


def p():
    print("a")

if __name__ == __main__:
    scheduler = BlockingScheduler()
    trigger = CronTrigger(second=1/5)
    scheduler.add_job(p, trigger)

    try:
        scheduler.start()
    except Exception as e:
        pass

经过上面修改后,应该可以正常运行了。如果你也有这样的问题,看看我的经验能不能帮到你?

以上是关于python3使用pyinstaller打包apscheduler出的错的主要内容,如果未能解决你的问题,请参考以下文章

python3使用pyinstaller打包apscheduler出的错

Python3使用Pyintaller-打包成exe

Python3快速入门(十八)——PyInstaller打包发布

python3.7 打包成exe的一种方法 pyinstaller

Python3中采用PyInstaller打包工程项目

Python3中采用PyInstaller打包工程项目