How to add MySQL Server bin path to System Path Variables using Python

70 Views Asked by At

I have created a automated script to install and start MySQL Server In which I have last step to add MySQL bin directory to System Variables

Here is snippet of that code:

try:
    command = f'setx PATH "%PATH%;{bin_path}"'

    process = subprocess.Popen(['runas', '/user:Administrator', 'cmd', '/c', command],
                               shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

    stdout, stderr = process.communicate()

    if process.returncode == 0:
        print("Directory added to PATH successfully.")
    else:
        print(f"Adding directory to PATH failed: {stderr}")
except Exception as e:
    print(f"An error occurred: {e}")

here bin_path is "C:\Program Files\MySQL\MySQL Server 8.0\bin"

The code seems to be correct but it is not working also by executing command in command prompt it does work but in python it is not working I don't know where i am going wrong

I tried the code mentioned above seems to work through normal command prompt but doesn't work using python subprocess. I want to solve the Add to PATH issue

0

There are 0 best solutions below