ahta14
ahta14

Reputation: 123

how to add windows system path with python?

I want to add folder to the windows path with python. I tried add folder with this codes.

path = os.path.dirname(os.path.abspath(__file__))
os.system('setx /M path "%path%;'+path+'"')

this code output is =>

ERROR: Access to the registry path is denied.

and I tried this code

os.environ['PATH'] += path

When I print this code, it prints the environment variables on the screen, but when I open the environment variables page, this value does not appear. How do I add this file path?

thanks.

Upvotes: 1

Views: 290

Answers (1)

Nitish Yadav
Nitish Yadav

Reputation: 55

As you can see the error which is:

ERROR: Access to the registry path is denied.

So, my suggestion would be running your program with the elevated permission. You can do so my using is 'admin' module

import admin
if not admin.isUserAdmin():
    admin.runAsAdmin()

And try running your code as admin user

Upvotes: 1

Related Questions