带有conda env的python windows_service在numpy上引发导入错误

问题描述

就像this person一样,我希望使用conda环境来提供Windows服务。

我的服务非常简单。我唯一想测试的就是pytorch是否正确导入:

sfdx force:apex:test:run --codecoverage -r json --classnames "Test_1,Test_2,.....,Test_10" -u testOrg --outputdir path/to/folder

我有一个conda环境,可以在powershell中使用它。

import socket

import servicemanager
import torch
import win32event
import win32service
import win32serviceutil


class SMWinservice(win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "TestService"
    _svc_description_ = "TestService delete later"

    @classmethod
    def parse_command_line(cls):
        win32serviceutil.HandleCommandLine(cls)

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None,None)
        socket.setdefaulttimeout(60)

    def SvcStop(self):
        self.stop()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        self.start()
        servicemanager.LogMsg(servicemanager.EVENTLOG_@R_949_4045@ION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_,''))
        self.main()

    def start(self):
        x = torch.empty(5,3)
        with open("mypath\\start.txt",'w') as file:
        file.write(x)

    def stop(self):
        with open("mypath\\stop.txt",'w') as file:
        file.write("stop")

    def main(self):
        with open("mypath\\main.txt",'w') as file:
        file.write("main")

if __name__ == '__main__':
    SMWinservice.parse_command_line()

现在我安装了该服务:

conda activate my_env
python
>>>import torch
>>>torch.empty(5,3)

这很好。现在,当我启动该服务时,我得到一个错误

python myService.py install

I am not the first one who has this error显然,许多人在安装了两个版本的numpy时都遇到了问题。 Python Could not import the service's module Traceback (most recent call last): File "my_path_to_project\serviceCondaTest\main.py",line 2,in <module> import torch File "my_conda_path\anaconda3\envs\my_env\lib\site-packages\torch\__init__.py",line 81,in <module> from torch._C import * ImportError: numpy.core.multiarray Failed to import 应该是找到正在使用的版本的方法,但是当我打印numpy版本时,会得到我想要的版本以及通往我的环境的正确路径。我找不到第二个Numpy版本。
怎么了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)