首页 > 解决方案 > 无法让 .py 程序在 Windows 10 上正常运行(它们在 Windows 7 上运行良好)。可能是权限问题

问题描述

几个月前,我用python创建了一个程序来自动编辑和处理我公司的成像技术人员拍摄的照片,以准备将它们发布到我们的网站上。通过 python 解释器运行程序本来是一种临时措施,直到我可以用更适合该应用程序的语言重写代码并一起获得适当的二进制文件,但是我弹出了其他项目,结果比预期的要慢,因此,我们的技术人员仍在双击 .py 文件来编辑他们的图像,而不是 .exe 文件。这不是问题,直到我们设置了两个新的成像站,都运行 Windows 10(其他站,程序构建和测试的地方,都运行 Windows 7),我被告知在这些新站上安装我的程序. 除非我以管理员身份运行它,否则什么都不会运行。我能够解决一些问题,但不能解决其他问题,并且真的可以使用一些帮助。以下是程序、问题和一些相关代码示例的细分,希望能让那里的人知道我应该做什么。

所以这个应用程序包含两个文件。一个是作为 Windows 服务安装的守护程序,它在后台运行,查看文件夹中的扫描和照片,然后在看到图像时运行一系列编辑功能并将编辑后的文件保存到另一个文件夹。第二个是一个简单的 GUI,它允许成像技术启动和停止守护进程,并在必要时进行手动编辑。核心文件工作正常。他们已经在 8 个成像站上运行了近 9 个月,没有出现任何问题。问题在于,在这些新的 Windows 10 系统上,程序以某种方式被“锁定”在它们运行所需的系统资源之外。只要我以管理员身份启动命令提示符,我就能够安装守护程序(否则我会收到“拒绝访问”错误),但一旦安装它就不会运行。每当我尝试启动它时,它就会启动并立即再次停止。此外,我可以以相同的方式启动 GUI(通过以管理员身份打开 Shell 的命令行),但无法通过双击打开它,或者将文件拖到 python.exe 上。在这两种情况下,就像守护程序一样,它会再次启动和停止。

如前所述,守护程序和 GUI 已安装并在运行 Windows 7 的几台计算机上运行。这不是 GUI 未打开,因为我没有启动循环或类似的东西。这里发生了一些奇怪的事情,我不知道它是什么,但这似乎与 Windows 7 和 Windows 10 之间的一些安全设置差异有关,但这只是猜测。如果有人知道真正的问题可能是什么,或者我可以尝试解决一些问题,那就太好了。

由于我不确定问题的原因是什么,因此我发布了与操作系统直接交互的每个部分的最小化代码,以及从我的文档中复制的安装说明。衷心感谢您提供的任何帮助。

守护进程

class SMWinservice(win32serviceutil.ServiceFramework):

    """This is a windows service that will watch the scan daemon folder and process 
    jpg images as they are scanned.
    """
    _svc_name_ = 'Scan Daemon'
    _svc_display_name_ = 'Scan Daemon'

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

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


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

    def SvcDoRun(self):
        self.start()
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_, ''))
        self.main()

    def start(self):
        self.isrunning = True


    def stop(self):
        self.isrunning = False

    def main(self): 

        image_list=[]
        d=0
        def rotncrop(file):                          
            im=comiccrop(filename, r'C:\Users\%s\Pictures\My Scans\processed\comic_%d.jpg' %(desktop, d))
            image_list.append(im)
            os.rename(filename,r'C:\Users\%s\Pictures\My Scans\originals\image_%d.jpg' %(desktop, d))
        while self.isrunning:                       
            for filename in glob.glob(r'C:\Users\%s\Pictures\My Scans\scan daemon\*.jpg' %desktop):
                file_size = os.stat(filename).st_size
                if file_size > 100000:
                    d=d+1
                    time.sleep(1)
                    rotncrop(filename)
            time.sleep(0.5)
if __name__ == '__main__':
   SMWinservice.parse_command_line()

图形用户界面

class maingui:
    def __init__(self, master):
        self.master=master
        master.title("Virtual Straight Edge")

        self.v = tkinter.StringVar()
        self.lotsofentries=Entry(master,textvariable=self.v)
        self.lotsofentries.pack(side='somewhere')

        self.lotsofbuttons=Button(text='do things', command=self.somefunction)
        self.lotsofbuttons.pack(side='top')

    def somefunction(self):
        stuff happens

root = Tk()
theta=0
gui = maingui(root)
gui.updatetext()
root.mainloop()

安装说明

1. Download anaconda python for windows.
2. Copy the “Virtual Straight Edge” folder into AppData\Local\Continuum\anaconda3.
3. Open the start menu and right click on “Computer”.
4. Scroll down and select “Properties”.
5. Select “Advanced System Settings”.
6. Select “Environment Variables”.
7. Scroll down to “PATH” and select “Edit”.
8. Add the “Virtual Straight Edge” folder and “anaconda3\python.exe” to your path, separated by semi-colons.
9. If on a scanner, double click on “setup.py”. If on a photostation, double click on “photostation setup.py”. When asked what program to use to run this file, select “Browse” and navigate to the anaconda3 folder. Select “python.exe” and check the box that says “Always use this program to open this type of file.”
10. Allow the setup to run. It should take less than a second. Navigate to the Pictures\My Scans folder and verify that four new folders have been created, named “processed”, “originals”, “VSE Images”, and “scan daemon” (or “photo daemon”).
11. Open the command prompt and change directories to the anaconda3 folder.
12. If on a scanner, install the daemons as windows services by typing “python.exe daemon.py install” and “python.exe cropdaemon.py install”. If on a photostation, type “python.exe photodaemon.py install” and “python.exe cropdaemon (photostation).py install”.
13. Press Ctrl+R and type services.msc in the entry field. This will open the Windows Services Manager. Verify that the “Scan Daemon” (or “Photo Daemon”) and “Crop Daemon” are listed as windows services and close the Windows Services Manager.
14. Create a shortcut to Virtual_Straight_Edge.py and add it to the My Scans folder and the desktop if desired. You are now set up to use Virtual Straight Edge!

标签: pythonpython-3.xwindowspowershellservice

解决方案


推荐阅读