18

I am trying to learn programming through Python, so I apologize in advance if this is an absurdly simple question.

I am attempting to simplify my convoluted directory structure and utilize some of Python's code-reuse features, I have encountered what is for me an inexplicable ImportError error. For the past several hours I've been reading about Python's import, module, and package features (here, here, here, and here among others), yet I remain unable to solve this (seemingly) simple error.

Here is the problem.

I have one directory (dir), within which resides one sub-directory (subdir). Each directory includes a few files. Thus, my overall directory structure looks like the following:

dir/
    __init__.py
    draw_lib.py
    subdir/
        __init___.py
        drawing.py

In my drawing.py file, I attempt to import draw_lib.py with the following line: from dir import daw_lib.py. It results in an ImportError: No module named dir. Can anyone provide a quick explanation for why my drawing.py file can't find my dir directory? Thank you for any assistance. I'm completely lost and would really like to improve my code reuse and directory structure once and for all.

Community
  • 1
  • 1
Bee Smears
  • 803
  • 3
  • 12
  • 22

1 Answers1

8

You are initially executing draw_lib.py. So the 'root directory' is / throughout the program.

Then, when you attempt 'from dir import draw_lib.py' in drawing.py it wont work because the root directory is still / and not dir/.

import draw_lib

Will work in drawing.py.

Example:

/
    __init__.py
    main.py
    test/
        __init___.py
        case.py

In main.py, put this:

import test.case

print 'main.py'

if __name__ == "__main__":
        test.case.test()

In test/case.py, put this:

import main

def test():
        print 'case.py'

My output:

main.py
main.py
case.py

As you can see, I imported main.py from a nested file. You'll see main.py two times. Once for the initial startup the second time when you import it in case.py.

cpb2
  • 798
  • 1
  • 9
  • 17
  • tried it. when I import `draw_lib`, I get the same error: `ImportError: No module named draw_lib`. Would that it were so simply :) – Bee Smears Jun 09 '14 at 21:02
  • 1
    Works here. I added an example to my answer :) – cpb2 Jun 09 '14 at 21:11
  • it doesn't work for me. I followed it word for word. any recommendations? could it be a screwy `PythonPath`? (either way, thanks for all of your help. these inexplicable, hours-long, seemingly-simple errors can be disheartening so, as someone who's just learning, I really appreciate the SO and other python communities.) – Bee Smears Jun 09 '14 at 23:34
  • 1
    How are you executing the script? Are you in the same directory as main.py? What OS is this on? And don't worry, we've all been trough the programming hours where everything seems to fail. – cpb2 Jun 10 '14 at 10:12
  • Windows OS. I am executing the script from the command line in the `test/` directory. – Bee Smears Jun 10 '14 at 13:19
  • You should be in the root directory. Then do 'python main.py'. – cpb2 Jun 10 '14 at 14:30
  • okay, brilliant. it works when I do that, but can one only ever import from subdirectories -- i.e., down the directory tree -- or is there a way to do what I was attempting to do? also, thanks a million for your help. I will accept your answer. – Bee Smears Jun 10 '14 at 14:53
  • 2
    No, there is no way to do what you originally wanted to do. From a design perspective there is no point in doing that. The only 2 ways to import a 'foreign' module is if its locatable within the root directory OR if it is installed as a module into Python's module directory (/usr/lib/python2.7/). I've never had to import the 'main' Python file from a second, nested Python file. It complicates things. Your 'main' should import modules and use them, not the other way around :) – cpb2 Jun 10 '14 at 15:15