Alex Zel
Alex Zel

Reputation: 688

Install windows drivers using python

I'm trying to automate driver installation using a python script, previously i was using a batch file for the same task, since i'm now building a GUI using python i'd like to incorporate everything into python.

I was using pnputil.exe to install driver: 'pnputil -i -a path_to_inf' but for some reason i can't make it work in python, i've tried subprocess.call, os.system, but nothing works, i always get some kind of error, using os.system i can run registry commands to read/write/add/remove keys, but with pnputil it just gives me errors.

os.system error = 'pnputil' is not recognized as an internal or external command, operable program or batch file.

subprocess.call error = subprocess.Popen(['pnputil -i -a path_to_inf'], shell=True) = The filename, directory name or volume label syntax is incorrect.

Upvotes: 2

Views: 7818

Answers (2)

Sam Morgan
Sam Morgan

Reputation: 3328

I ran across this issue myself and banged my head against it for quite a while. Putting this up here for anyone else that hits the issue as I wasn't able to find a good explanation of why this problem was occurring.

This problem happens because a 32-bit Python application is attempting to access a 64-bit Windows resource and is being automatically redirected to the incorrect path. More info on that relationship here:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

So, if your Python installation is 64-bit, you're already in the right context, and just a call to pnputil should work. If you're on 32-bit Python, you must redirect to sysnative. The following is the code I'm currently using to grab a list of driver store drivers:

import platform
import subprocess

if '32bit' in platform.architecture():
    process = '%systemroot%\Sysnative\pnputil.exe -e'
else:
    process = 'pnputil.exe -e'

p = subprocess.Popen(process, shell=True,
        stdout=subprocess.PIPE, universal_newlines=True)

Upvotes: 2

Sailesh Kotha
Sailesh Kotha

Reputation: 2099

You have to use the whole address of pnputil.exe to execute in python..

Try this

subprocess.Popen(['C:\\Windows\\System32\\PNPUTIL.exe -i -a path_to_inf'], shell=True) 

Or

subprocess.Popen(['C:\\Windows\\SYSNATIVE\\PNPUTIL.exe -i -a path_to_inf'], shell=True)

Either should work, because it is based on 32-bit and 64-bit version

Upvotes: 4

Related Questions