module importing itself

Viewed 13801

I'm trying to import a module from an application-specific terminal (Maya in this case, but eventually others). I've downloaded a project off git, and I have a structure like so:

modulename
    submodule
        __init.py__
        subsubmodule
        ...
    submodule
    ...
    __init.py__
    modulename.py

Then in my execution shell, I'm trying to import the module to use on the shell, so I have:

import sys,os
modulepath = 'C:/path/to/module'
sys.path.append(modulepath)
import modulename

If the modulename.py being imported is blank, everything is fine. The first two lines of the module are, however, the module trying to import itself (I don't really know why, seeing as this is someone elses project, but it seems to be important to the structure)

import sys,os
import modulename from modulename as mod

This gets the error:

# Error: ImportError: cannot import name modulename# 

And nothing else can proceed.

Ultimately I'm wondering why a module can't import itself, and/or how to get around that problem?

I have read that a module thinks of itself as being named main so that relative imports don't work, in which case I would expect

import __main__ as mod

to work in its stead, which also does not, with error:

# Error: ImportError: Cannot re-init internal module __main__ #

Which makes total sense.

[The question is, how can I import a module from within itself?]

1 Answers
Related