Commit 35ac1dbc43b967c1407ddfcc8fa3db44b2fe4c2f

Authored by decalage2
1 parent d8d79443

olemeta: fixed absolute imports (issue #141)

Showing 1 changed file with 19 additions and 5 deletions
oletools/olemeta.py
... ... @@ -15,7 +15,7 @@ http://www.decalage.info/python/oletools
15 15  
16 16 #=== LICENSE =================================================================
17 17  
18   -# olemeta is copyright (c) 2013-2016, Philippe Lagadec (http://www.decalage.info)
  18 +# olemeta is copyright (c) 2013-2017, Philippe Lagadec (http://www.decalage.info)
19 19 # All rights reserved.
20 20 #
21 21 # Redistribution and use in source and binary forms, with or without modification,
... ... @@ -47,8 +47,9 @@ http://www.decalage.info/python/oletools
47 47 # 2016-09-06 v0.50 PL: - added main entry point for setup.py
48 48 # 2016-10-25 PL: - fixed print for Python 3
49 49 # 2016-10-28 PL: - removed the UTF8 codec for console display
  50 +# 2017-04-26 v0.51 PL: - fixed absolute imports (issue #141)
50 51  
51   -__version__ = '0.50'
  52 +__version__ = '0.51'
52 53  
53 54 #------------------------------------------------------------------------------
54 55 # TODO:
... ... @@ -59,9 +60,22 @@ __version__ = '0.50'
59 60  
60 61 #=== IMPORTS =================================================================
61 62  
62   -import sys, codecs
63   -from .thirdparty import olefile
64   -from .thirdparty.tablestream import tablestream
  63 +import sys, os, codecs
  64 +
  65 +# IMPORTANT: it should be possible to run oletools directly as scripts
  66 +# in any directory without installing them with pip or setup.py.
  67 +# In that case, relative imports are NOT usable.
  68 +# And to enable Python 2+3 compatibility, we need to use absolute imports,
  69 +# so we add the oletools parent folder to sys.path (absolute+normalized path):
  70 +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  71 +# print('_thismodule_dir = %r' % _thismodule_dir)
  72 +_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..'))
  73 +# print('_parent_dir = %r' % _thirdparty_dir)
  74 +if not _parent_dir in sys.path:
  75 + sys.path.insert(0, _parent_dir)
  76 +
  77 +from oletools.thirdparty import olefile
  78 +from oletools.thirdparty.tablestream import tablestream
65 79  
66 80  
67 81 #=== MAIN =================================================================
... ...