Commit d8d794437830d0cfb38e4898b70cde734cd4f6fb

Authored by decalage2
1 parent 54ca41c8

oleid: fixed absolute imports (issue #141)

Showing 1 changed file with 16 additions and 14 deletions
oletools/oleid.py
... ... @@ -18,7 +18,7 @@ http://www.decalage.info/python/oletools
18 18  
19 19 #=== LICENSE =================================================================
20 20  
21   -# oleid is copyright (c) 2012-2016, Philippe Lagadec (http://www.decalage.info)
  21 +# oleid is copyright (c) 2012-2017, Philippe Lagadec (http://www.decalage.info)
22 22 # All rights reserved.
23 23 #
24 24 # Redistribution and use in source and binary forms, with or without modification,
... ... @@ -53,6 +53,7 @@ from __future__ import print_function
53 53 # 2014-11-30 v0.03 PL: - improved output with prettytable
54 54 # 2016-10-25 v0.50 PL: - fixed print and bytes strings for Python 3
55 55 # 2016-12-12 v0.51 PL: - fixed relative imports for Python 3 (issue #115)
  56 +# 2017-04-26 PL: - fixed absolute imports (issue #141)
56 57  
57 58 __version__ = '0.51'
58 59  
... ... @@ -77,19 +78,20 @@ __version__ = '0.51'
77 78  
78 79 import optparse, sys, os, re, zlib, struct
79 80  
80   -try:
81   - # Relative imports (only works when imported from package):
82   - from .thirdparty import olefile
83   - from .thirdparty.prettytable import prettytable
84   -except:
85   - # if it does not work, fall back to absolute imports:
86   - # add this module's folder to sys.path (absolute+normalized path):
87   - _thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
88   - if not _thismodule_dir in sys.path:
89   - sys.path.insert(0, _thismodule_dir)
90   - # absolute imports:
91   - from thirdparty import olefile
92   - from thirdparty.prettytable import prettytable
  81 +# IMPORTANT: it should be possible to run oletools directly as scripts
  82 +# in any directory without installing them with pip or setup.py.
  83 +# In that case, relative imports are NOT usable.
  84 +# And to enable Python 2+3 compatibility, we need to use absolute imports,
  85 +# so we add the oletools parent folder to sys.path (absolute+normalized path):
  86 +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  87 +# print('_thismodule_dir = %r' % _thismodule_dir)
  88 +_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..'))
  89 +# print('_parent_dir = %r' % _thirdparty_dir)
  90 +if not _parent_dir in sys.path:
  91 + sys.path.insert(0, _parent_dir)
  92 +
  93 +from oletools.thirdparty import olefile
  94 +from oletools.thirdparty.prettytable import prettytable
93 95  
94 96  
95 97  
... ...