Importlib: One directory is a package, while another isn't?

This isn’t strictly-speaking a Panda issue; I’m nevertheless hoping that someone here will have some insight on it.

So, I’m attempting to use “importlib” for a certain feature of my game, calling “importlib.import_module” to dynamically load some modules.

However, while it works for one of my modules, in one hierarchy of directories, for some reason it doesn’t work for another module in a parallel hierarchy of directories!

In the case in which it doesn’t work, this is the error that I’m seeing:

<context above this point omitted for brevity and clarity>
  File "/home/thaumaturge/Documents/My Game Projects/FantasySpaceShooter/", line 21, in loadMoon
    module = importlib.import_module("Content.{0}.{1}.{2}.{2}".format(contentSet, MOON_DIR, moonID))
  File "/usr/lib/python3.6/importlib/", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'Content.TestDLC.Moons.DLCMoon.DLCMoon'; 'Content.TestDLC.Moons.DLCMoon' is not a package

Specifically, and as you can see in the error above, my call to “import_module” looks something like this:
importlib.import_module("Content.{0}.{1}.{2}.{2}".format(value1, value2, value3))

That call then corresponds to a hierarchy that looks something like this:

    --- value1/
           --- value2/
                  --- value3/

All of the folders involved have “” files, and all of the folders and files involved do exist, I do believe.

Specifically, here is an abridged image showing the two directories:

With these files and directories, if I’m not much mistaken the following call works:
While this call doesn’t:

Does anyone know what might be going wrong?

The working directory may have been changed.

Hmm… Even within a single IDE session? I can get these results between two consecutive runs of my program (by changing a parameter that determines which module is loaded), with no restarting of the IDE.

If so, what might have changed it?

I just assumed, mind you, I’m not claiming.

Ah, fair enough! I do appreciate the suggestion!

Can you reproduce this with a minimal sample (a basic that does just the importing, together with the intended directory and file structure, all added to a .zip file)?

Strangely, in my attempts to do so, I find that when I move the directory structure over to the location of a small test-program and then attempt to run the importation from that test-program, it works for both trees.

This seems to suggest that there’s something different between the environment in which those calls are being made in my actual code–but what that might be I don’t know. Perhaps something to do with earlier interactions that the code has with the directories…

Ah, I believe that I’ve found the problem!

It turns out that there was a “.pyc” file from a previous iteration of the folder-hierarchy yet remaining, and that “importlib” was apparently drawing form that, rather than from the actual folder-hierarchy! Running a clean-up of such files seems to have resulted in the importation working as expected!

(I discovered this by adding a breakpoint to my program and then attempting to run importations in the watch-list. This led me to notice that, when I imported the problem path without its final section–i.e. “Content.TestDLC.Moons.DLCMoon” instead of “Content.TestDLC.Moons.DLCMoon.DLCMoon”–the module was imported–and indicated to have been drawn from a “.pyc” file.)

Thank you to all who posted here! :slight_smile: