0

Does importing a specific function from a module is a faster process than importing the whole module?

That is, is from module import x debugs faster than import module?

3 Answers 3

1

I would say there is little or no peformance difference, as importing a module for the first time will execute the entire module - all classes, variables and functions are all built, regardless of the actually symbol you need.

The 2nd time you import the module in the same program that will be much quicker, as the module is not reloaded, and all existing definitions are used.

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

Comments

1

No, it shouldn't be faster, and that shouldn't matter anyway: importing things is not usually considered a performance-critical operation, so you can expect it to be fairly slow compared to other things you can do in Python. If you require importing to be very fast, probably something is wrong with your design.

Comments

1

The whole module has to compile before you can import the specific function.

Instead, it's just a difference in namespace. (ie. you call module_x.function_y vs just calling function_y)

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.