Commit b460e8e2d983961bc25e552aa363f186afccf0c4

Authored by decalage2
1 parent 1feffdc3

oletimes: fixed absolute imports (issue #141)

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