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@grief Functions to help using pywin32. 

4""" 

5 

6from __future__ import print_function 

7 

8import sys 

9import os 

10import shutil 

11 

12if sys.version_info[0] == 2: 

13 FileNotFoundError = Exception 

14 

15 

16def import_pywin32(): 

17 """ 

18 For the module ``pywin32``, 

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

20 before throwing the exception: 

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

22 

23 It checks the instruction ``import win32com``. 

24 """ 

25 try: 

26 import win32com 

27 except ImportError as e: 

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

29 import os 

30 import sys 

31 import numpy 

32 from distutils.sysconfig import get_python_lib 

33 

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

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

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

37 os.path.join( 

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

39 ]) 

40 

41 epath = os.environ["PATH"] 

42 for path in paths: 

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

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

45 

46 try: 

47 import win32com 

48 return win32com 

49 except ImportError: 

50 # we try the next path 

51 pass 

52 

53 try: 

54 import win32com 

55 return win32com 

56 except ImportError: 

57 # addition for WinPython 

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

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

60 try: 

61 import win32com 

62 return win32com 

63 except ImportError: 

64 dll = os.listdir(path) 

65 dll = [os.path.join(path, _) for _ in dll if "dll" in _] 

66 if len(dll) == 0: 

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

68 else: 

69 raise ImportError( 

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

71 else: 

72 raise e 

73 

74 

75def fix_pywin32_installation(python_path=None, fLOG=print): 

76 """ 

77 copy DLL at the right place 

78 

79 @param python_path python path 

80 @param fLOG logging function 

81 

82 .. faqref:: 

83 :title: pywin32 does not work 

84 

85 To check module `pywin32 <https://pypi.python.org/pypi/pywin32>`_ is installed, 

86 you must run:: 

87 

88 import win32com 

89 

90 If it displays the message ``ImportError: DLL load failed``, it means 

91 it was not able to find DLLs *pythoncom34.dll*, *pythoncom34.dll*. 

92 Two solutions: 

93 

94 * Add the folder ``C:\\Python34_x64\\Lib\\site-packages\\pywin32_system32`` 

95 to environment variable ``PATH``. That's what function 

96 @see fn import_pywin32 is doing every time it is called. 

97 * Copy the two DLLs to ``C:\\Windows\\System32``, that's what function 

98 @see fn fix_pywin32_installation does if it is run with admin rights. 

99 

100 .. versionadded:: 1.1 

101 """ 

102 if python_path is None: 

103 python_path = sys.executable.replace("w.exe", ".exe") 

104 if os.path.isfile(python_path): 

105 python_path = os.path.dirname(python_path) 

106 fdll = os.path.join(python_path, "Lib", 

107 "site-packages", "pywin32_system32") 

108 dest_fold = [python_path, os.path.join(python_path, "DLLs"), 

109 "C:\\Windows\\System32"] 

110 for dll in os.listdir(fdll): 

111 full = os.path.join(fdll, dll) 

112 if os.path.isdir(full): 

113 continue 

114 for destf in dest_fold: 

115 dest = os.path.join(destf, dll) 

116 if not os.path.exists(dest): 

117 shutil.copy(full, destf) 

118 fLOG("[pymy] copy", full, "to", destf) 

119 else: 

120 fLOG("[pymy] already copied", dest)