- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个可以作为 Windows 服务安装的项目,但我在完成它时遇到了麻烦。
Venv 已为此项目做好准备,并安装了 pywin32 软件包(版本 227)。然而,当我尝试从控制台运行 python 文件时:
import win32serviceutil
我收到以下错误:
ModuleNotFoundError: No module named 'win32'
我尝试过的事情:
python -m pip install pywin32
重新安装将导入方式更改为:
从 win32 导入 win32serviceutil
从 win32.lib 导入 win32serviceutil
导入 win32.lib.win32serviceutil 作为 win32serviceutil
来自this thread的回答
win32 被 PyCharm 识别为文件夹:
奇怪的是,我可以运行以下命令并安装 Windows 服务:
python MyPythonFile.py install
它不会返回任何错误。但是尝试使用命令启动服务:
python MyPythonFile.py start
返回:
"Error 1053: The service did not respond to the start or control request in a timely fashion"
在 Debug模式下python MyPythonFile.py debug
它返回:
ModuleNotFoundError: No module named 'win32serviceutil'
最佳答案
该线程的解决方案有效: Using PythonService.exe to host python service while using virtualenv
我用来解决它的代码:
import os
import sys
service_directory = os.path.dirname(__file__)
source_directory = os.path.abspath(service_directory)
os.chdir(source_directory)
venv_base = os.path.abspath(os.path.join(source_directory, "..", "..", "venv"))
sys.path.append(".")
old_os_path = os.environ['PATH']
os.environ['PATH'] = os.path.join(venv_base, "Scripts")+ os.pathsep + old_os_path
site_packages = os.path.join(venv_base, "Lib", "site-packages")
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = venv_base
new_sys_path = list()
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
此代码必须在导入错误之前运行
关于python - 模块未找到错误: No module named 'win32serviceutil' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59047849/
我是一名优秀的程序员,十分优秀!