Hướng dẫn how do i run a python executable on startup? - làm cách nào để chạy tệp thực thi python khi khởi động?

import winreg

def set_autostart_registry(app_name, key_data=None, autostart: bool = True) -> bool:
    """
    Create/update/delete Windows autostart registry key

    ! Windows ONLY
    ! If the function fails, OSError is raised.

    :param app_name:    A string containing the name of the application name
    :param key_data:    A string that specifies the application path.
    :param autostart:   True - create/update autostart key / False - delete autostart key
    :return:            True - Success / False - Error, app name dont exist
    """

    with winreg.OpenKey(
            key=winreg.HKEY_CURRENT_USER,
            sub_key=r'Software\Microsoft\Windows\CurrentVersion\Run',
            reserved=0,
            access=winreg.KEY_ALL_ACCESS,
    ) as key:
        try:
            if autostart:
                winreg.SetValueEx(key, app_name, 0, winreg.REG_SZ, key_data)
            else:
                winreg.DeleteValue(key, app_name)
        except OSError:
            return False
    return True


def check_autostart_registry(value_name):
    """
    Check Windows autostart registry status

    ! Windows ONLY
    ! If the function fails, OSError is raised.

    :param value_name:  A string containing the name of the application name
    :return: True - Exist / False - Not exist
    """

    with winreg.OpenKey(
            key=winreg.HKEY_CURRENT_USER,
            sub_key=r'Software\Microsoft\Windows\CurrentVersion\Run',
            reserved=0,
            access=winreg.KEY_ALL_ACCESS,
    ) as key:
        idx = 0
        while idx < 1_000:     # Max 1.000 values
            try:
                key_name, _, _ = winreg.EnumValue(key, idx)
                if key_name == value_name:
                    return True
                idx += 1
            except OSError:
                break
    return False

Tạo Autostart:

set_autostart_registry('App name', r'C:\test\x.exe')

Cập nhật Autostart:

set_autostart_registry('App name', r'C:\test\y.exe')

Xóa Autostart:

set_autostart_registry('App name', autostart=False)

Kiểm tra Autostart:

if check_autostart_registry('App name'):

Cải thiện bài viết

Lưu bài viết

Thêm một tập lệnh Python vào Windows Start-up về cơ bản có nghĩa là tập lệnh Python sẽ chạy khi Windows khởi động. Điều này có thể được thực hiện theo quy trình hai bước -

Bước #1: Thêm tập lệnh vào thư mục khởi động Windows & NBSP; sau khi Windows khởi động, nó chạy (tương đương với nhấp đúp) tất cả các ứng dụng có trong thư mục khởi động của mình. & NBSP; 
After the windows boots up it runs (equivalent to double-clicking) all the application present in its startup directory. 

Địa chỉ: & nbsp; 

C: \ Users \ current_user \ appdata \ roaming \ Microsoft \ Windows \ start
 

Theo mặc định, thư mục AppData trong current_user được ẩn, vì vậy hãy bật các tệp ẩn để lấy nó và dán phím tắt của tập lệnh vào địa chỉ đã cho hoặc chính tập lệnh. Ngoài ra, mặc định của các tệp .py phải được đặt thành python ide khác, tập lệnh có thể sẽ mở dưới dạng văn bản thay vì thực thi. & Nbsp; & nbsp; & nbsp; Đúng, nó liên quan đến việc chỉnh sửa khóa Windows Registry HKEY_CURRENT_USER từ chính tập lệnh Python. Đăng ký này chứa danh sách các chương trình phải chạy khi người dùng đăng nhập. Cũng giống như một vài ứng dụng bật lên khi Windows bắt đầu vì sự thay đổi trong sổ đăng ký và thêm đường dẫn ứng dụng của chúng vào nó..PY files default must be set to python IDE else the script may end up opening as a text instead of executing. 
  
Step #2: Adding script to windows Registry 
This process can be risky if not done properly, it involves editing the windows registry key HKEY_CURRENT_USER from the python script itself. This registry contains the list of programs that must run once the user Login. just like few application which pops up when windows starts because the cause change in registry and add their application path to it.

Đường dẫn đăng ký:

HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ currentVersion \ Run

Dưới đây là mã Python: & NBSP;

Python3

import winreg as reg

import os            

def set_autostart_registry('App name', r'C:\test\x.exe')0

set_autostart_registry('App name', r'C:\test\x.exe')1set_autostart_registry('App name', r'C:\test\x.exe')2set_autostart_registry('App name', r'C:\test\x.exe')3 set_autostart_registry('App name', r'C:\test\x.exe')4

set_autostart_registry('App name', r'C:\test\x.exe')1set_autostart_registry('App name', r'C:\test\x.exe')6set_autostart_registry('App name', r'C:\test\x.exe')3set_autostart_registry('App name', r'C:\test\x.exe')8    

set_autostart_registry('App name', r'C:\test\x.exe')1set_autostart_registry('App name', r'C:\test\y.exe')0set_autostart_registry('App name', r'C:\test\x.exe')3set_autostart_registry('App name', r'C:\test\y.exe')2

set_autostart_registry('App name', r'C:\test\x.exe')1set_autostart_registry('App name', r'C:\test\y.exe')4set_autostart_registry('App name', r'C:\test\x.exe')3 set_autostart_registry('App name', r'C:\test\y.exe')6

set_autostart_registry('App name', r'C:\test\x.exe')1set_autostart_registry('App name', r'C:\test\y.exe')8set_autostart_registry('App name', r'C:\test\x.exe')3 set_autostart_registry('App name', autostart=False)0

set_autostart_registry('App name', r'C:\test\x.exe')1set_autostart_registry('App name', autostart=False)2 set_autostart_registry('App name', r'C:\test\x.exe')3 set_autostart_registry('App name', autostart=False)4set_autostart_registry('App name', autostart=False)5set_autostart_registry('App name', autostart=False)6

set_autostart_registry('App name', r'C:\test\x.exe')1set_autostart_registry('App name', autostart=False)8set_autostart_registry('App name', autostart=False)2if check_autostart_registry('App name'):0if check_autostart_registry('App name'):1if check_autostart_registry('App name'):0set_autostart_registry('App name', autostart=False)5if check_autostart_registry('App name'):4

set_autostart_registry('App name', r'C:\test\x.exe')1if check_autostart_registry('App name'):6set_autostart_registry('App name', autostart=False)2if check_autostart_registry('App name'):8

if check_autostart_registry('App name'):9 import0set_autostart_registry('App name', r'C:\test\x.exe')3set_autostart_registry('App name', r'C:\test\x.exe')3import3import4

set_autostart_registry('App name', r'C:\test\x.exe')1import6

Lưu ý: Có thể thêm các mã tiếp theo vào tập lệnh này để nhiệm vụ được thực hiện ở mỗi lần khởi động và tập lệnh phải được chạy với tư cách quản trị viên lần đầu tiên. & NBSP;Further codes can be added to this script for the task to be performed at every startup and the script must be run as Administrator for the first time.
 


Python có thể được chạy như một thực thi?

Có, có thể biên dịch các tập lệnh Python thành các tệp thực thi độc lập.Pyinstaller có thể được sử dụng để chuyển đổi các chương trình Python thành các tệp thực thi độc lập, trong Windows, Linux, Mac OS X, FreeBSD, Solaris và AIX.. PyInstaller can be used to convert Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris, and AIX.

Bạn có thể chạy một con trăn exe mà không có python?

PY2EXE là một tiện ích mở rộng Python chuyển đổi các tập lệnh Python (. PY) thành Microsoft Windows Executables (.exe).Các tệp thực thi này có thể chạy trên một hệ thống mà không cần cài đặt Python.Nó là công cụ phổ biến nhất để làm như vậy.These executables can run on a system without Python installed. It is the most common tool for doing so.