Without Me It Just Aweso
Without Me It Just Aweso

Reputation: 4853

python import from a sub-sub directory

I have a folder structure as:

mainapp.py
+Level1
   __init__.py
   +Level2
       helper.py
       __init__.py
       utilities.py
       testapp.py

In mainapp.py I wish to import helper.py. I am able to do that using

import Level1.Level2.helper

helper.py is imported, but in trying to resolve helper.py's imports it fails. IN this case helper.py has

from Level2 import utilities

Which throws an

ImportError: No module named Level2

What am I doing wrong? Is it the way I'm trying to import? All the examples I found were only trying to go one level deep, and I tried following the same guidance to no avail.

I am using Python3

EDIT: LEVEL1 and below are external library I downloaded. I cant modify them. In level2 is a testapp that comes with the library that uses both helper and utilities. Running this test app works.

But I need to be able to use these files from my main app that lives 2 folders up

EDIT2: I believe my problem stems from my working directory being the directory that mainapp.py lives in. When I import using the fully qualified path LEvel1.Level2.helper that works, but once helper attempts to import Level2, there is no level2 in my current working directory. Is there a way to alias this? Or somehow have their imports relative to their location, not my working directory?

Upvotes: 1

Views: 1998

Answers (2)

dreyescat
dreyescat

Reputation: 13798

When you run a python script it is important from where you are executing the script because that path becomes one of the relative paths from where the imports (all imports) are going to search for the packages/modules. I'm talking about the sys.path.

So, knowing nothing else about the library, you could simply add the Level1 path into the sys.path and sort of expose internal Level2 sub-package to the world.

Assuming you are in a bash shell in the same folder as your mainapp.py you can add the Level1 path to sys.pah using PYTHONPATH:

$ export PYTHONPATH=./Level1

Then helper.py will be able to find the utilities module even executing your mainapp.py from outside.

Opinion:

I don't know the library and how it works, but in my opinion, importing from the same package using the package name itself it's not correct or at least not a good practice. And here you have an example, you can only import that package if you are in the same level or in the parent one. And probably the test app is working because, as you say, it is in the same package (level) that the utilities.

Original answer:

What you need is called Intra-package References. In your specific case as utilities and helper are in the same level you need:

from . import utilities

Upvotes: 2

ErlVolton
ErlVolton

Reputation: 6784

helper.py is inside the Level2 module, so that's implied in its import context. The import line in helper.py should instead be

import utilities

Alternatively, you could add the path to Level1 to your PYTHONPATH. This will allow you to directly import all modules under Level1 without additional namespace, e.g.

from Level2 import utilities

Which sounds like what you need, based on your edit. There is probably a third option involving patching sys.modules, but that would involve patching sys.modules.

Upvotes: 1

Related Questions