Importing modules in Python - best practice

I am new to Python as I want to expand skills that I learned using R. In R I tend to load a bunch of libraries, sometimes resulting in function name conflicts.

What is best practice in Python. I have seen some specific variations that I do not see a difference between

import pandas, from pandas import *, and from pandas import DataFrame

What are the differences between the first two and should I just import what I need. Also, what would be the worst consequences for someone making small programs to process data and compute simple statistics.

UPDATE

I found this excellent guide. It explains everything.


Disadvantage of each form

When reading other people's code (and those people use very different importing styles), I noticed the following problems with each of the styles:

import modulewithaverylongname will clutter the code further down with the long module name (e.g. concurrent.futures or django.contrib.auth.backends) and decrease readability in those places.

from module import * gives me no chance to see syntactically that, for instance, classA and classB come from the same module and have a lot to do with each other. It makes reading the code hard. (That names from such an import may shadow names from an earlier import is the least part of that problem.)

from module import classA, classB, functionC, constantD, functionE overloads my short-term memory with too many names that I mentally need to assign to module in order to coherently understand the code.

import modulewithaverylongname as mwvln is sometimes insufficiently mnemonic to me.

A suitable compromise

Based on the above observations, I have developed the following style in my own code:

import module is the preferred style if the module name is short as for example most of the packages in the standard library. It is also the preferred style if I need to use names from the module in only two or three places in my own module; clarity trumps brevity then ("Readability counts").

import longername as ln is the preferred style in almost every other case. For instance, I might import django.contrib.auth.backends as djcab. By definition of criterion 1 above, the abbreviation will be used frequently and is therefore sufficiently easy to memorize.

Only these two styles are fully pythonic as per the "Explicit is better than implicit." rule.

from module import xx still occurs sometimes in my code. I use it in cases where even the as format appears exaggerated, the most famous example being from datetime import datetime (but if I need more elements, I will import datetime as dt).


import pandas imports the pandas module under the pandas namespace, so you would need to call objects within pandas using pandas.foo.

from pandas import * imports all objects from the pandas module into your current namespace, so you would call objects within pandas using only foo. Keep in mind this could have unexepcted consequences if there are any naming conflicts between your current namespace and the pandas namespace.

from pandas import DataFrame is the same as above, but only imports DataFrame (instead of everything) into your current namespace.

In my opinion the first is generally best practice, as it keeps the different modules nicely compartmentalized in your code.


In general it is better to do explicit imports. As in:

import pandas
frame = pandas.DataFrame()

Or:

from pandas import DataFrame
frame = DataFrame()

Another option in Python, when you have conflicting names, is import x as y:

from pandas import DataFrame as PDataFrame
from bears import DataFrame as BDataFrame
frame1 = PDataFrame()
frame2 = BDataFrame()

Here are some recommendations from PEP8 Style Guide.

  1. Imports should usually be on separate lines, e.g.:

    Yes: import os
         import sys
    
    No:  import sys, os
    

    but it is okay to

    from subprocess import Popen, PIPE
    
  2. Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

    • Imports should be grouped in the following order:
      1. standard library imports
      2. related third party imports
      3. local application/library specific imports
    • You should put a blank line between each group of imports.
  3. Absolute imports are recommended
    They are more readable and make debugging easier by giving better error messages in case you mess up import system.

    import mypkg.sibling
    from mypkg import sibling
    from mypkg.sibling import example
    

    or explicit relative imports

    from . import sibling
    from .sibling import example
    
  4. Implicit relative imports should never be used and is removed in Python 3.

    No:  from ..grand_parent_package import uncle_package
    
  5. Wildcard imports ( from <module> import * ) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.


Some recommendations about lazy imports from python speed performance tips.

Import Statement Overhead

import statements can be executed just about anywhere. It's often useful to place them inside functions to restrict their visibility and/or reduce initial startup time. Although Python's interpreter is optimized to not import the same module multiple times, repeatedly executing an import statement can seriously affect performance in some circumstances.

the given below is a scenario explained at the page,

>>> def doit1():
... import string
... string.lower('Python')
...
>>> import string
>>> def doit2():
... string.lower('Python')
...
>>> import timeit
>>> t = timeit.Timer(setup='from __main__ import doit1', stmt='doit1()')
>>> t.timeit()
11.479144930839539
>>> t = timeit.Timer(setup='from __main__ import doit2', stmt='doit2()')
>>> t.timeit()
4.6661689281463623

from A import B

essentially equals following three statements

import A
B = A.B
del A

That's it, that is it all.