Python import question

I tried googling the answer to my predicament, but I am not finding the answer.

I am having an import issue where I have multiple subdirectories with modules of the same name, that do slightly different things. I want to be able to import only modules from a specific subdirectory.

for instance I have


module 0 and module 1 both have the line

from module_a import *

But currently is finding the /sub0/ instead of /sub1/

It is not looking in its own subdirectory, I think because is really the root, and it just starts combing through until it finds the first.

Any ideas, other than renaming the modules?

you confused me there a bit. how do the import lines for module0 and module1 look like in

do this in

import sub0.module0 as sub00
import sub1.module1 as sub11
YCW = sub00.YourClassesWhatever()

in short:
try to use as few ‘*’ imports as possible and don’t use the form

from sub0 import module_a

as you might accidentially overwrite it with

from sub1 import module_a


but seriously, it’s WAY easier and more pythonic to give your modules unique names

I can see how its confusing, but basically it all start out like this.
I had a project that did two completely different things, driving, and shooting. We wanted to split these two things up into two separate projects, so essentially I duped the code, and stripped the functionality out of the respective projects, so now we have driving in one, and shooting in the other.

The majority of the modules are identical, but some of them differ slightly now, like the terrain handler for instance now does not need some of the functionality in shooting, as it did in driving.

We put these two separate projects into the DemoMaster framework, so that we could select between the two and launch them at runtime.

The problem is, my second project seems to be importing modules from the first project, although they are in completely separate subdirectories.

The directory structure actually looks more like this


The DemoMaster class imports both Driving and Shooting.

My main class for both Driving and Shooting uses

from TerrainHandler import *

I assumed that it would attempt to load the TerrainHandler module from the subdirectory it is in, but that is clearly not the case.

I hope this cleared things up a little.

You could try something like this, perhaps:

from __future__ import absolute_import

from .TerrainHandler import *
# Yes, the leading dot was not a typo

Not entirely sure about it, read more here for more info about relative imports.