importing symbols from python package into caller's namespace
Posted
by Paul C
on Stack Overflow
See other posts from Stack Overflow
or by Paul C
Published on 2010-06-02T15:24:37Z
Indexed on
2010/06/02
19:04 UTC
Read the original article
Hit count: 202
I have a little internal DSL written in a single Python file that has grown to a point where I would like to split the contents across a number of different directories + files.
The new directory structure currently looks like this:
dsl/
__init__.py
types/
__init__.py
type1.py
type2.py
and each type file contains a class (e.g. Type1
).
My problem is that I would like to keep the implementation of code that uses this DSL as simple as possible, something like:
import dsl
x = Type1()
...
This means that all of the important symbols should be available directly in the user's namespace. I have tried updating the top-level __init__.py
file to import the relevant symbols:
from types.type1 import Type1
from types.type2 import Type2
...
print globals()
the output shows that the symbols are imported correctly, but they still aren't present in the caller's code (the code that's doing the import dsl
). I think that the problem is that the symbols are actually being imported to the 'dsl' namespace. How can I change this so that the classes are also directly available in the caller's namespace?
© Stack Overflow or respective owner