Skip to content

Commit

Permalink
Support expanding user (~) and variables ($HOME) in import_file
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Jun 17, 2020
1 parent c0b323e commit 22b951d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pyutilib/misc/import_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def import_file(filename, context=None, name=None, clear_cache=False):
This function returns the module object that is created.
"""

# Support shell-style paths like ~user and $HOME/bin
filename = os.path.expanduser(os.path.expandvars(filename))
#
# Parse the filename to get the name of the module to be imported
# and determine if it contains any directory information about
Expand All @@ -63,9 +65,7 @@ def import_file(filename, context=None, name=None, clear_cache=False):
# For 2.4 compatibility we will call endswith() twice.
if modulename.endswith('.py') or modulename.endswith('.pyc'):
if not os.path.exists(filename):
if not os.path.exists(os.path.expanduser(filename)):
raise IOError("File %s does not exist" % (filename))
filename = os.path.expanduser(filename)
raise IOError("File %s does not exist" % (filename))
if filename.endswith('.pyc'):
filename = filename[:-1]
modulename = modulename.rsplit('.', 1)[0]
Expand Down

0 comments on commit 22b951d

Please sign in to comment.