如何让我的程序在python中自动运行

时间:2013-12-17 14:43:10

标签: python-2.7

我通过pyinstaller导出我的python代码我需要我的程序自动启动Windows(我不需要通过启动文件夹这样做)

1 个答案:

答案 0 :(得分:1)

首先,您需要创建一个快捷方式。这将在您的桌面上创建快捷方式

import os, sys
import pythoncom
from win32com.shell import shell, shellcon

shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
  None,
  pythoncom.CLSCTX_INPROC_SERVER,
  shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)

desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)

您可以为启动文件创建任意窗口位置(或仅在启动位置创建它):

  • 运行一次:HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrentVersion \ RunOnce
  • 运行每个开始:HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrentVersion \ Run
  • StartUp文件夹:C:\ Documents and Settings \ All Users \ Start Menu \ Programs \ Startup
  • 共享任务管理器:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Explorer \ SharedTaskScheduler
  • 将其设为服务,并自动启动该服务


 要查看计算机上自动启动的程序或轻松添加条目,您可以使用SysInternals中的 autoruns http://technet.microsoft.com/en-us/sysinternals/bb963902.aspx

P.S。 Python示例来自timgolden.me.uk网站。

相关问题