Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (47.6k points)

I've tried to find a comprehensive guide on whether it is best to use import module or from module import? I've just started with Python and I'm trying to start off with best practices in mind.

Basically, I was hoping if anyone could share their experiences, what preferences other developers have and what's the best way to avoid any gotchas down the road?

1 Answer

0 votes
by (106k points)

Actually, the only difference between import module and from module import something  is subjective. You can use any of them and be consistent in your use of it. 

These are some of the advantages and disadvantages of both import and from module import:-

What are the pros and cons of the import module in Python:-

  • The Pros of the import module is as follows:

It saves you from adding any additional imports you can directly start using another item from the module.

  • The Cons of the import module is as follows:

If you will write  module.function name in your code this can be tedious and redundant 

What are the pros and cons of from module import xyz in Python:-

The Pros of from module import something is as follows:

  • You need to write less to use xyz module.

  • You always have more control what items a module can be.

The Cons of from module import something is as follows:

  • When you want to use any new item from the module you have to update your import statement

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...