ImportError on tilde path


I had a Python module named located inside ~/foobar directory. To be able to import it, I did this in my Python code:

import sys
import foobar

However, when that script was run, it gave this error:

Traceback (most recent call last):
  File "./", line 10, in 
    import foobar
ImportError: No module named foobar


It turns out that Python cannot expand the tilde to my home directory! This was quite surprising to me. Once I expanded it to the absolute path, the import worked fine.

Tried with: Python 2.7.12 and Ubuntu 16.04

2 thoughts on “ImportError on tilde path

  1. Small precision for the unaware reader (as I was few time ago) : Β«expand to the absolute pathΒ» means :

    In my scripts I quite often initiate with
    and then use os.path.join(HOME, )


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.