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

/
main.py
  /sub0
    module0.py
    module_a.py
  /sub1
    module1.py
    module_a.py

module 0 and module 1 both have the line

from module_a import *

But currently module1.py is finding the /sub0/module_a.py instead of /sub1/module_a.py

It is not looking in its own subdirectory, I think because main.py 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 main.py?

do this in main.py:

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

/c/DemoMaster/
  demomaster.py
    /Driving_Demo
      Driving.py
      TerrainHandler.py
    /Shooting_Demo
      Shooting.py
      TerrainHandler.py 

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.