4

I was reading on the difference between from … import vs import ... in python 3.6. after multiple search I can't find any answer to my specific question.

Look at the following example:

from urllib import request

import urllib

1) Are they equal? I suppose from the basic meaning in English language that the first one imports only request and the seconds imports everything inside urllib including request and many others. Am I right?

2) I 1 is true, suppose my python program uses only request from urllib would their be any performance advantages on using the first instead of the second?

2

2 Answers 2

5

It almost entirely depends on context, style guide (for example the google style guide defines this), and developer but there are some guidelines I personally follow.

  1. If the name of the functions you are importing can have more than 1 meaning, use a import module style. For example if a module has a name like select() this can mean many things. If it comes from a sql library it may be a select statement, i've also seen the same name used in libraries that take user input etc. Basically follow the Zen of python

    Explicit is better than implicit

  2. If there is more than one usage of the same name in your dependencies/standard library. For example in both the math and secrets module have a function caalled choice, but only the one in the secrets module is designed to be used in cryptography so it's better to import secrets and use secrets.choice or math.choice instead so you know which you are using.
  3. If you (or your team) use different terminology, it can be better to do from module import function as blah. So long as this terminology is consistent this can be a good way to organize your imports. For example if your team always refers to random.randint as randy you can do from random import randint as randy (this is a bad example, it's just what I thought of off the top of my head), though this means anyone outside your organization will not understand necessarily what the code does.
  4. DO NOT USE from module import * this is always a terrible idea as it brings in EVERYTHING and can cause name collisions with in-file functions.
  5. If libraries have names you want to use built in then using from module import blah can be useful to avoid naming collisions.
  6. This is a somewhat padantic point, and honestly unless you are importing a ridiculous number of things it doesn't matter but importing select functions is actually faster because of how python initializes interpreter sessions. This only matters on MASSIVE scale though.

I could go on for ages, but as @Mayank mentioned there is already another good answer posted here: Use 'import module' or 'from module import'?

1

When using import urllib Will import every method inside of the urllib package but you will have to call it as so:

urllib.request.urlopen('http://python.org/') as response:

which means for each method you will want to use you will have to call the package name. However when using from urllib import request you can use the method directly as so:

request.urlopen('http://python.org/') as response:

The best practice is to import only the methods since when you do a blind import you are essentially adding a lot of overhead to your code that is not needed. But it does not really matter which method you choose.