Jump to content

Problem with importing

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Hey,

so I'm upgrading my package structure. I finally found out that by importing with __import__ instead of the regular import, I don't have to know the name of the package before hand. Here's the problem: the original code works:
import top_pkg.desc_pkg
import fact

def fun():
[INDENT]top_pkg.desc_pkg.cls = fact.new()
[/INDENT]
but the new version does not:
mod_name = __import__(__name__)
import fact

def fun():
[INDENT]mod_name.cls = fact.new()
[/INDENT] 
Both of the code's are in a package's __init__.py. For some reason when later calling the module's cls-attribute, it doesn't exist.

Any ideas why ??

#2
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
This problem just got solved.
The __import__ only returned the top package name, but giving it the fromlist-argument solves that issue. After that it sort of works.
So it'd go like this:
curr_mod = __import__(__name__,fromlist=[''])