我将脚本作为服务运行时遇到问题。
我不知道如何处理路径。
我所拥有的:
main.py (main code) service.py (template to install and run main.py
/ | \
subdir/ subdir/ config.ini
script1.py script2.py
main.py中的内容:
1.我读了config.ini
并获得了带有路径的变量,即:
[XLS]
dir = D:\\ExcelToSQL
workbook1 = workbook1.xls
workbook2 = workbook2.xls
workbook3 = workbook3.xls
[SQL]
user = root
password = password
host = localhost
database = database
port = 3306
ssl_ca =
ssl_verify_cert =
ssl_key =
ssl_cert =
encoding =
MySQL DB
的script2再次起作用。
所有变量都来自config.ini
调试服务时的主要问题。它无法读取config.ini
:
Error 0xC0000003 - The instance's SvcRun() method failed
Traceback (most recent call last):
File "C:\Python\lib\site-packages\win32\lib\win32serviceutil.py", line 839, in SvcRun
self.SvcDoRun()
File "D:\rs_al\IdeaProjects\ExcelToSQL\PyXLSQL\service.py", line 31, in SvcDoRun
self.main()
File "D:\rs_al\IdeaProjects\ExcelToSQL\PyXLSQL\service.py", line 37, in main
app()
File "D:\rs_al\IdeaProjects\ExcelToSQL\PyXLSQL\pyxlsql.py", line 14, in app
path = config.get ("XLS", "dir")
File "C:\Python\lib\configparser.py", line 780, in get
d = self._unify_values(section, vars)
File "C:\Python\lib\configparser.py", line 1146, in _unify_values
raise NoSectionError(section) from None
configparser.NoSectionError: No section: 'XLS'
(null): (null)
我知道问题出在哪里:
在main.py中:
print(sys.argv[0])
输出
C:\Python\lib\site-packages\win32\PythonService.exe
是否可以根据需要运行服务?因为现在它正在C:\ Python \ lib \ site-packages \ win32 \ PythonService.exe中寻找ini
文件。