【问题标题】:How can I turn a python file into a exe file withou this error如何在没有此错误的情况下将 python 文件转换为 exe 文件
【发布时间】:2021-03-14 22:03:57
【问题描述】:

嗯,我在做一个个人项目,对于这个项目,我需要每次在电脑上运行一个python脚本,而这个脚本需要在电脑开机的时候启动,为此,我使用了PyInstaller来开启“.exe”文件中的脚本,但显示错误。 Python 代码,文件名; “teste.py”:

import pynotifier

pynotifier.Notification(
    "TESTE",  # Translate: Test
    "ISSO É APENAS UM TESTE",  # Translate: This is just a test
    6
).send()

这段代码只是给我一个通知,我用这段代码把它变成了一个“.exe”文件:

pyinstaller --onefile --nowindowed teste.py

当我执行它时,将python文件正常转换为exe文件,但是当我执行它时,它显示了这个消息:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "threading.py", line 917, in _bootstrap_inner
  File "threading.py", line 865, in run
  File "win10toast\__init__.py", line 93, in _show_toast
  File "pkg_resources\__init__.py", line 1144, in resource_filename
  File "pkg_resources\__init__.py", line 357, in get_provider      
  File "pkg_resources\__init__.py", line 900, in require
  File "pkg_resources\__init__.py", line 786, in resolve
pkg_resources.DistributionNotFound: The 'win10toast' distribution was not found and is required by the application

更多信息,这是 PyNotifier 模块中的代码,因为它在网络上没有文档:

init.py 文件:

from .pynotifier import Notification

版本.py文件:

VERSION = (0, 1, 1)
__version__ = '.'.join(map(str, VERSION))

pynotifier.py 文件:

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

# PyNotifier
# Copyright (c) 2018 Yuriy Lisovskiy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__all__ = ['Notification']

# standard library
import platform


# class to run notification
class Notification:

    # urgency level
    URGENCY_LOW = 'low'
    URGENCY_NORMAL = 'normal'
    URGENCY_CRITICAL = 'critical'

    # 'title' - a title of notification
    # 'description' - more info about the notification
    # 'duration' - notification timeout in seconds
    # 'urgency' - notification urgency level
    # 'icon_path' - path to notification icon file
    def __init__(self, title, description, duration=5, urgency=URGENCY_LOW, icon_path=None):
        if urgency not in [self.URGENCY_LOW, self.URGENCY_NORMAL, self.URGENCY_CRITICAL]:
            raise ValueError('invalid urgency was given: {}'.format(urgency))
        self.__WINDOWS = 'Windows'
        self.__LINUX = 'Linux'
        self.__title = title
        self.__description = description
        self.__duration = duration
        self.__urgency = urgency
        self.__icon_path = icon_path
        self.__is_windows = False

    # 'send' - sends notification depending on system
    def send(self):
        system = platform.system()
        if self.__LINUX in system:
            self.__send_linux()
        elif self.__WINDOWS in system:
            self.__send_windows()
        else:
            raise SystemError('notifications are not supported for {} system'.format(system))

    # '__send_linux' - sends notification if running on Linux system
    def __send_linux(self):
        import subprocess
        command = [
            'notify-send', '{}'.format(self.__title),
            '{}'.format(self.__description),
            '-u', self.__urgency,
            '-t', '{}'.format(self.__duration * 1000)
        ]
        if self.__icon_path is not None:
            command += ['-i', self.__icon_path]
        subprocess.call(command)

    # '__send_windows' - sends notification if running on Windows system
    def __send_windows(self):
        try:
            import win10toast
            win10toast.ToastNotifier().show_toast(
                threaded=True,
                title=self.__title,
                msg=self.__description,
                duration=self.__duration,
                icon_path=self.__icon_path
            )
        except ImportError:
            raise ImportError('notifications are not supported, can\'t import necessary library')

请帮帮我:|

【问题讨论】:

    标签: python file pyinstaller exe


    【解决方案1】:

    试试这个库的

    import plyer.platforms.win.notification
    from plyer import notification
    

    【讨论】:

    • 哥们,我试着去图书馆找找,但是没有找到关于平台包的任何东西,我试着搜索通知,但是什么也没有,:(
    • 兄弟,谢谢,我知道了,你救了我的皮肤
    • 我只是有一个问题,我不知道如何更改通知中的“Python”._.
    • 我不能,我需要声望 ;-;
    猜你喜欢
    • 1970-01-01
    • 2022-11-21
    • 2015-03-26
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    相关资源
    最近更新 更多