【发布时间】:2020-09-24 10:51:23
【问题描述】:
几天以来,我一直在寻找解决方案,但没有成功。 我们有一个 Windows 服务版本,可以将一些文件从一个位置复制到另一个位置。
所以我使用 Python 3.7 构建了如下所示的代码。 完整的编码可以在Github找到。
当我使用 python 运行服务时一切正常,我可以安装服务并启动服务。
这个使用命令:
安装服务:
- python jis53_backup.py 安装
运行服务:
- python jis53_backup.py 启动
当我现在使用带有命令的 pyinstaller 编译这段代码时:
- pyinstaller -F --hidden-import=win32timezone jis53_backup.py
创建 exe 后,我可以安装服务,但尝试启动服务时出现错误:
错误启动服务:服务没有响应启动或 及时控制请求
我在 Stackoverflow 和 Google 上浏览了多篇与此错误相关的帖子,但均未成功。我没有选择在需要运行此服务的 PC 上安装 python 3.7 程序。这就是我们尝试构建 .exe 的原因。
我已确保根据我在不同问题中找到的信息更新路径。
路径定义图片:
我还复制了 pywintypes37.dll 文件。
来自 -> Python37\Lib\site-packages\pywin32_system32
到 -> Python37\Lib\site-packages\win32
有没有人对如何使它工作有任何其他建议?
'''
Windows service to copy a file from one location to another
at a certain interval.
'''
import sys
import time
from distutils.dir_util import copy_tree
import servicemanager
import win32serviceutil
import win32service
from HelperModules.CheckFileExistance import check_folder_exists, create_folder
from HelperModules.ReadConfig import (check_config_file_exists,
create_config_file, read_config_file)
from ServiceBaseClass.SMWinService import SMWinservice
sys.path += ['filecopy_service/ServiceBaseClass',
'filecopy_service/HelperModules']
class Jis53Backup(SMWinservice):
_svc_name_ = "Jis53Backup"
_svc_display_name_ = "JIS53 backup copy"
_svc_description_ = "Service to copy files from server to local drive"
def start(self):
self.conf = read_config_file()
if not check_folder_exists(self.conf['dest']):
create_folder(self.conf['dest'])
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
while self.isrunning:
# Copy the files from the server to a local folder
# TODO: build function to trigger only when a file is changed.
copy_tree(self.conf['origin'], self.conf['dest'], update=1)
time.sleep(30)
if __name__ == '__main__':
if sys.argv[1] == 'install':
if not check_config_file_exists():
create_config_file()
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(Jis53Backup)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(Jis53Backup)
【问题讨论】:
-
这个运气好吗?
-
@njho,到目前为止还没有运气。我可以通过 python 运行服务,但无法通过 exe 运行。
-
嘿@peter 感谢您的回复。我还在玩我的,但我已经让它工作了一半。检查 Python 环境变量是否也在您的 SYSTEM 路径中。这就是为我解决的问题。让我知道它是否有效
-
@njho,我的系统路径中有 python,但它仍然不适合我。
-
这个运气好吗?我有同样的问题。想知道它是否是 python3.7 的东西......因为有很多使用 3.6 的例子
标签: python python-3.x pyinstaller