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?

2
  • 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? Commented Aug 30, 2017 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 Commented Aug 30, 2017 at 11:45

4 Answers 4

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()).

Sign up to request clarification or add additional context in comments.

6 Comments

It looks like __import()__ will work perfectly in this case
@noamt Actually, you really shouldn't use __import__. If you want something like it, use importlib.import_module.
Does __import__ or importlib handle fully qualified paths with class names in them? e.g. module.OuterClass.InnerClass.some_classmethod?
@mgilson Looks like it, yeahhttps://stackoverflow.com/questions/8790003/dynamically-import-a-method-in-a-file-from-a-string
@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.
|
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.

Comments

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)

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.