Importing characters from a python package into caller namespace

I have a small internal DSL written in one Python file, which has grown to such an extent that I would like to split the contents into several different directories + files.

The new directory structure now 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 the code that uses this DSL as simple as possible, for example:

import dsl
x = Type1()
...

This means that all important characters must be available directly in the user namespace. I tried updating the top-level file __init__.pyto import the corresponding characters:

from types.type1 import Type1
from types.type2 import Type2
...
print globals()

, , (, import dsl). , , dsl. , ?

+3
4

from dsl import *
+2

,

dsl/init __. py

def import_symbols(namespace):
    namespace['type1'] = dsl.types.type1
    namespace['type2'] = dsl.types.type2

, do

import dsl

dsl.import_symbols(globals())

, , import_symbols(), , , import *.

+1

. , . , from dsl import *, .

0

@Eli, @ "". , ...

: , "" dsl ( ), from dsl import *. , , .

from pkg import * , , , , , , .

0

Source: https://habr.com/ru/post/1748175/


All Articles