Skip to content

Instantly share code, notes, and snippets.

@audy
Created June 28, 2011 02:49
Show Gist options
  • Save audy/1050384 to your computer and use it in GitHub Desktop.
Save audy/1050384 to your computer and use it in GitHub Desktop.
relative imports

I am trying to do relative imports in Python

In Ruby, I can just

require '../puppies.rb'

But Python gets all weird when I try something similar.

My structure looks like this:

project/
    lib/
       something_awesome.py
    bin/
       exe.py

I want to import something_awesome.py into exe.py

However:

from ..lib import something_awesome

results in,

Traceback (most recent call last):  File "bin/exe.py", line 9, in <module>
    from ..lib import something_awesome
ValueError: Attempted relative import in non-package

How do I do this? The only answers on Stack Overflow are of the "you're not supposed to do that" sort.

@davclark
Copy link

http://docs.python.org/2/tutorial/modules.html#intra-package-references ?

Looks like maybe you're missing an empty init.py file (a common gotcha).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment