Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Functions to help using pywin32 

4""" 

5 

6 

7def import_pywin32(): # pragma: no cover 

8 """ 

9 For the module ``pywin32``, 

10 this function tries to add the path to the DLL to ``PATH`` 

11 before throwing the exception: 

12 ``DLL load failed: The specified module could not be found``. 

13 """ 

14 try: 

15 import win32com 

16 return win32com 

17 except ImportError as e: 

18 if "DLL load failed:" in str(e): 

19 import os 

20 import sys 

21 import numpy 

22 from distutils.sysconfig import get_python_lib 

23 

24 paths = set([os.path.join( 

25 os.path.split(sys.executable)[0], "Lib", "site-packages", "pywin32_system32"), 

26 os.path.join(get_python_lib(), "pywin32_system32"), 

27 os.path.join( 

28 os.path.dirname(numpy.__file__), "..", "pywin32_system32"), 

29 ]) 

30 

31 epath = os.environ["PATH"] 

32 last_path = None 

33 for path in paths: 

34 last_path = path 

35 # exe = os.path.abspath(os.path.dirname(sys.executable)) 

36 os.environ["PATH"] = epath + ";" + path 

37 

38 try: 

39 import win32com 

40 return win32com 

41 except ImportError: 

42 # we try the next path 

43 continue 

44 

45 try: 

46 import win32com 

47 return win32com 

48 except ImportError: 

49 # addition for WinPython 

50 exe = os.path.abspath(os.path.dirname(sys.executable)) 

51 os.environ["PATH"] = os.environ["PATH"] + ";" + exe 

52 try: 

53 import win32com 

54 return win32com 

55 except ImportError: 

56 dll = os.listdir(last_path) 

57 dll = [os.path.join(last_path, _) 

58 for _ in dll if "dll" in _] 

59 if len(dll) == 0: 

60 raise ImportError("Did you install pywin32?") from e 

61 raise ImportError( 

62 "Some DLL must be copied:\n" + "\n".join(dll)) from e 

63 else: 

64 raise e