如何从 Python 中的 Windows 服务获取绝对文件路径?

How to get absolute file path from an Windows Service in Python?

我在 Python 中提供了一项 windows 服务。这工作正常。但是我需要获取 安装 我的服务的文件路径。我尝试使用以下代码:

os.get getcwd()

Returns:

Windows System32 folder

os.path.realpath(__file__)

Returns:

NameError: global name 'file' is not defined

如何获取我的 windows 服务应用程序的文件路径?

使用sys.prefix获取用于EXE的文件夹。 当使用 py2exe 等实用程序创建 EXE 文件夹时,它保存着 EXE 的文件夹。

例如

sys.path 将保存到 python24.zip

的路径