2

I see the question I am going to ask is a very trivial one and has been asked and consequently answered by many. I have looked through the solution provided for the problem, however I don't see the solution to work in my case.

I have the following directory hierarchy.

----a
-------__init__.py
--------------aa
------------------aa.py
------------------__init__.py
--------------bb
------------------bb.py
------------------__init__.py
-------a.py

I would like to do the following.

  1. To import bb.py and aa.py from the file a.py-- accomplished by using __init__.py in each folder directory
  2. TO import bb.py from aa.py (Doesn't work)
  3. TO import a.py from aa.py (Doesn't work)

By looking at many solutions I have placed the __init__.py file inside every folder directory.

I used:

import imp
foo = imp.load_source('module.name', 'path to the file') 

This worked, but since the path has to be hard-coded I am not sure if this will be a viable solution in my case

Currently I am doing the imports by adding the path to the sys directories. I'd like a solution that's more dynamic.

The folder hierarchy of my project goes deep to 6-7 levels sub-directories and I need a solution to import a module at level 1 from level 7.

I'll be glad if somebody could point out what I am missing.

zondo
20.4k8 gold badges50 silver badges89 bronze badges
asked May 25, 2016 at 11:02
4
  • 1
    You probably want to name your init files __init__.py. Commented May 25, 2016 at 11:09
  • 2
    "The folder hierarchy of my project goes deep to 6-7 levels sub-directories". Simplistic judgement, but that's just bad; you may want to rethink your structure. Commented May 25, 2016 at 11:11
  • The init files are named correctly. I dont know while I type __ with init, the __ gets removed automatically Commented May 25, 2016 at 11:21
  • The project is huge with lots and lots of reusable components, If I try to structure in differently, I may end up creating a mess Commented May 25, 2016 at 11:23

1 Answer 1

1

What's the entry point of your application? If, for example, you start from command line the aa.py file, you are not lucky.

"Guido views running scripts within a package as an anti-pattern" (rejected PEP-3122)

The entry point of you package should be somewhere in the root of the package. In your case it's a file a.py. From out there you import some internal modules as usual:

# contents of a.py
from aa import a

Now about the imports from within your package.

Let's say there is one more file aa1.py in the same directory as aa.py. To import it from aa.py you can use relative import feature

# contents of aa.py
from . import aa1

Here . means "in the same directory".

Other examples:

# contents of aa.py
from .. import a # here .. means "one level above"
from ..bb import bb # this will import your bb.py module

Please note, that all that imports will fail if for example you run from command line directly aa.py!

UPDATE:

. and .. in above examples are NOT names of directories. This is python syntax to say "same level", "one level above", etc. If you need to import from 3 levels above you should write:

from .... import my_module
answered May 25, 2016 at 12:11
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Lesnik, Thanks for the Answer. However, the solution here will work for this case in particular. How do I go about, when I have to import a file at level 1 from a file thats at level 4 or 5
Since the project is not yet released, I have to debug some modules. Therefore the entry point could be at any point.
Entry point must be on root level. I think there is a reason why this is a must. F.e. you start from aa.py and import some module aa1 which is on the same level as aa. Name of the module would be "aa1". Now you import some module from above (a.py), that module again imports you aa1.py. Now it's name would be aa.aa1. It's not good. All your modules within a package should have persistent names. Naming scheme used in python is: package_name.subdir1.subdir2.module_file_name. You need to import a module from 4 levels above: write "from ..... import my_module"
And even if you need to debug some module deep inside you package you need to start importing from the root level. For exactly same purpose I used to use a "test.py" file on top level. Much better approach is to use python unittests: your tests would import the modules to test starting from top-level: "from a.aa import aa" or something like that.
Oh Okay,, I guess, I will have to use the test and debugging modules in the top of the hierarchy. Thank you so much..

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.