7

In Java I can invoke a class or method without importing it by referencing its fully qualified name:

public class Example {
    void example() {

        //Use BigDecimal without importing it
        new java.math.BigDecimal(1);
    }
}

Similar syntax will obviously not work using Python:

class Example:
    def example(self):

        # Fails
        print(os.getcwd())

Good practice and PEP recommendations aside, can I do the same thing in Python?

noamt
  • 7,397
  • 2
  • 37
  • 59
  • 1
    No, you can't invoke a class or method without importing it -- Why do you want to avoid the import? You can import it locally in your function which just changes your java 1-liner into a 2-liner in python. Is that the only thing that you're trying to avoid? – mgilson Aug 30 '17 at 11:39
  • I fancy the one-liners because I'm playing around with some AST-based code injection and I'd like to modify the original module as little as possible – noamt Aug 30 '17 at 11:45

4 Answers4

4

A function does not exist until its definition runs, meaning the module it's in runs, meaning the module is imported (unless it's the script you ran directly).

The closest thing I can think of is print(__import__('os').getcwd()).

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • It looks like `__import()__` will work perfectly in this case – noamt Aug 30 '17 at 12:01
  • 1
    @noamt Actually, you really shouldn't use `__import__`. If you want something like it, use `importlib.import_module`. – Christian Dean Aug 30 '17 at 12:03
  • Does `__import__` or `importlib` handle fully qualified paths with class names in them? e.g. `module.OuterClass.InnerClass.some_classmethod`? – mgilson Aug 30 '17 at 12:17
  • @mgilson Looks like it, yeahhttps://stackoverflow.com/questions/8790003/dynamically-import-a-method-in-a-file-from-a-string – noamt Aug 30 '17 at 12:33
  • @noamt -- Based on the answers I'm seeing there, it looks like the answer is "No", it doesn't handle that case. The answers all do some processing of the string to split it into a path part and a `fromlist` part. – mgilson Aug 30 '17 at 12:43
  • @mgilson Well it is still fully qualified, only split at the method level – noamt Aug 30 '17 at 13:40
2

No. If you want to use a module in Python, you must explicit import it's name into the scope. And, as @AlexHall mentioned, a class/function/module does not exist until import time. There's no way to accesses it without import-ing. In my opinion however, this makes for better and more explicit code. This forces you to be explicit when importing module names.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
2

Very late, but in case someone finds it useful, I've been using:

def fqn_import(fqn: str):
    module_name, _, function_name = fqn.rpartition('.')
    return getattr(importlib.import_module(module_name), function_name)
VBobCat
  • 2,527
  • 4
  • 29
  • 56
1

I'm not sure that you can do exactly the same, but you can import only the function:

from foo.bar import baz as baz_fn

baz_fn()

where foo.bar is the fully qualified name of the module that contains the function and baz is the name of the function you wish to import. It will import it as the name baz_fn.

Luftzig
  • 523
  • 4
  • 14