Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 631
John Avatar asked Mar 28 '12 22:03

John


People also ask

What is correct way to import a Python module?

Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.

What happens when you import a module Python?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.

Where should I put my Python modules?

For the most part, modules are just Python scripts that are stored in your Lib or Lib/site-packages folder, or local to the script being run. That's it.


1 Answers

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

like image 96
Lutz Prechelt Avatar answered Oct 06 '22 23:10

Lutz Prechelt