I’m attempting to use the SQL Server SMO library from Python 2.7 using pyWin32. I can import win32com, but I’ve been stymied in any attempt to access the library. The code I am attempting is below.
import sys
sys.path.append(r'C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies')
import win32com.client
server = win32com.client.Dispatch('Microsoft.SqlServer.Management.SMO.Server')
def main():
print server
if __name__ == '__main__':
main()
When this code is run, I get pywintypes.com_error: (-2147221005, 'Invalid class string', None, None).
It seems likely that I am simply getting the library’s name wrong in the call to Dispatch, but I can’t figure out any way to know what it should be.
It seems like this may actually be a path problem.
This works:
import win32api
win32api.LoadLibrary(r'C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Smo.dll')
However, this does not:
import sys
sys.path.append(r'C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies')
import win32api
win32api.LoadLibrary(r'Microsoft.SqlServer.Smo.dll')
win32com is for using COM libraries and it doesn’t know anything about .NET assemblies. Currently, there is no way to use .NET directly from CPython, so your options are to use IronPython or write a command line tool in C# or whatever and then call it from Python.