Top-level import in python output

I’m wondering how I can add a top-level import to the generated python? The python.Syntax.import* functions seem to add the import to the local function.

I don’t remember python target having generic “add top-level import” feature, however it supports automatic import generation for extern classes, would that be enough for you?

For example:

@:pythonImport("my_module")
extern class MyModule {
	static function method():Void;
}

@:pythonImport("my_module", "TheType")
extern class TypeFromModule {
	static function method():Void;
}

class Main {
	static function main() {
		MyModule.method();
		TypeFromModule.method();
	}
}

This will generate:

import my_module as MyModule
from my_module import TheType as TypeFromModule


class Main:
    __slots__ = ()

    @staticmethod
    def main():
        MyModule.method()
        TypeFromModule.method()

If you really just want to add an “import” (for the side-effect of it?), I think you can use the python.Syntax.import* methods inside a static function __init__() somewhere - this will be generated outside of all functions, however not at the top of a file, but at the bottom, where all static initialization is happening.

Hope that helps! :slight_smile:

Perfect! Thank you.