如何在 Windows 启动时启动 python 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4438020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:44:00  来源:igfitidea点击:

How to start a python file while Windows starts?

pythonwindows

提问by sam

I have a python file and I am running the file.

我有一个 python 文件,我正在运行该文件。

If Windows is shutdown and booted up again, how I can run that file every time Windows starts?

如果 Windows 关闭并再次启动,我如何在每次 Windows 启动时运行该文件?

回答by LiMuBei

Haven't tested this, but I'd create a batch file that contains "python yourfile.py" and put that in the autostart folder.

尚未对此进行测试,但我会创建一个包含“python yourfile.py”的批处理文件并将其放入自动启动文件夹中。

回答by darioo

In the following startup directory (at least this path exists on Windows XP):

在以下启动目录中(至少在 Windows XP 上存在此路径):

C:\Documents and Settings\All Users\Start Menu\Programs\Startup

put a shortcut to your python program. It should be executed every time your system starts up.

为你的python程序添加一个快捷方式。每次系统启动时都应该执行它。

回答by rob

Depending on what the script is doing, you may:

根据脚本正在执行的操作,您可以:

  1. package it into a service, that should then be installed
  2. add it to the windows registry (HKCU\Software\Microsoft\Windows\CurrentVersion\Run)
  3. add a shortcut to it to the startup folder of start menu - its location may change with OS version, but installers always have some instruction to put a shortcut into that folder
  4. use windows' task scheduler, and then you can set the task on several kind of events, including logon and on startup.
  1. 将它打包成一个服务,然后应该安装
  2. 将它添加到 Windows 注册表 (HKCU\Software\Microsoft\Windows\CurrentVersion\Run)
  3. 在开始菜单的启动文件夹中添加一个快捷方式 - 它的位置可能会随操作系统版本而变化,但安装程序总是有一些说明可以将快捷方式放入该文件夹
  4. 使用windows的任务调度器,然后你可以在几种事件上设置任务,包括登录和启动。

The actual solution depends on your needs, and what the script is actually doing.
Some notes on the differences:

实际的解决方案取决于您的需求以及脚本实际执行的操作。
关于差异的一些说明:

  • Solution #1 starts the script with the computer, while solution #2 and #3 start it when the user who installed it logs in.
  • It is also worth to note that #1 always start the script, while #2 and #3 will start the script only on a specific user (I think that if you use the default user then it will start on everyone, but I am not sure of the details).
  • Solution #2 is a bit more "hidden" to the user, while solution #3 leaves much more control to the user in terms of disabling the automatic start.
  • Finally, solution #1 requires administrative rights, while the other two may be done by any user.
  • Solution #4 is something I discovered lately, and is very straightforward. The only problem I have noticed is that the python script will cause a small command window to appear.
  • 解决方案 #1 在计算机上启动脚本,而解决方案 #2 和 #3 在安装它的用户登录时启动它。
  • 还值得注意的是,#1 总是启动脚本,而 #2 和 #3 只会在特定用户上启动脚本(我认为如果您使用默认用户,那么它会在每个人身上启动,但我不是确定细节)。
  • 解决方案#2 对用户来说有点“隐藏”,而解决方案#3 在禁用自动启动方面给用户留下了更多的控制权。
  • 最后,解决方案#1 需要管理权限,而其他两个可以由任何用户完成。
  • 解决方案#4 是我最近发现的,而且非常简单。我注意到的唯一问题是 python 脚本会导致出现一个小的命令窗口。

As you can see, it all boils down to what you want to do; for instance, if it is something for your purposes only, I would simply drag it into startup folder.

如您所见,这一切都归结为您想做什么;例如,如果它只是为了您的目的,我会将它拖到启动文件夹中。

In any case, lately I am leaning on solution #4, as the quickest and most straightforward approach.

无论如何,最近我倾向于使用解决方案#4,这是最快和最直接的方法。

回答by RedDeath

try adding an entry to "HKLM/SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" . Right click ->new -> string value -> add file path

尝试向 "HKLM/SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" 添加一个条目。右键->新建->字符串值->添加文件路径

回答by tzadok

if can simply add the following code to your script. Nevertheless, this only works on windows!:

如果可以简单地将以下代码添加到您的脚本中。尽管如此,这只适用于 Windows!:

import getpass
USER_NAME = getpass.getuser()


def add_to_startup(file_path=""):
    if file_path == "":
        file_path = os.path.dirname(os.path.realpath(__file__))
    bat_path = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup' % USER_NAME
    with open(bat_path + '\' + "open.bat", "w+") as bat_file:
        bat_file.write(r'start "" %s' % file_path)

this function create a bat file in the startup folder that run your script.

此函数在运行脚本的启动文件夹中创建一个 bat 文件。

the file_path is the path to the file that you would like you run when your computer opens. you can leave it blank in order to add the running script to startup.

file_path 是您希望在计算机打开时运行的文件的路径。您可以将其留空以将正在运行的脚本添加到启动中。

回答by Rahul

You can put run_script.cmd in

你可以把 run_script.cmd 放在

C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Content of run_script.cmd

run_script.cmd 的内容

python path\to\your\script.py

回答by rajat prakash

Above mentioned all the methods did not worked I tried them all , I will tell you more simpler solution and alternative of windows task scheduler

上面提到的所有方法都没有用我都试过了,我会告诉你更简单的解决方案和windows任务调度程序的替代方案

Create a .bat file with content "ADDRESS OF YOUR PROJECT INTERPRETER" "ADDRESS OF YOUR PYTHON SCRIPT WITH SCRIPT NAME"

创建一个 .bat 文件,内容为“您的项目解释器的地址”“带有脚本名称的 Python 脚本的地址”

Store this bat file into the window startup folder(by default hidden) FYI: to find window startup folder press windos+rthen type shell:startup-- it will directly take you to the startup folder

将此bat文件存储到窗口启动文件夹中(默认隐藏)仅供参考:要找到窗口启动文件夹按windos + r然后键入shell:startup- 它会直接带你到启动文件夹

copy the bat file there with following 2 address in the same format , then simply restart the system or shut down and boot up.

将bat文件复制到下面2个相同格式的地址,然后重启系统或者关机开机即可。

The code will automatically run within 20 seconds of opening.

代码将在打开后 20 秒内自动运行。

Thank me later

晚点再谢我