简体   繁体   中英

What is the most pythonic way to import modules in python

Can anyone suggest me what is the most pythonic way to import modules in python? Let me explain - i have read a lot of python code and found several different ways of how to import modules or if to be more precise - when to import:

  1. Use one module/several modules which include all the imports(third party modules) which are necessary for entire project so all of the imports are concentrated within few modules so it is easy to maintain imports. When any single module requires any module to be imported it ask references module for it. For example in our project we have separated level named 'references' so it contains modules like 'system.py'(contains references to all system libraries), 'platform.py'(contains references to all platform libraries), 'devexpress.py'(contains references to all devexpress libraries) and so on. These modules looks like:
  2. Each module imports all necessary classes and functions at the top of the module - eg there is a section with imports within each module in project
  3. Each function/class uses import locally eg right after definition and import only things that them really need.

Please find samples below.

1 sample import module - only 'import' and 'from ... import ...' statements(no any methods or classes):

#references.py
import re
import clr
import math

import System
import System.Text.RegularExpressions
import System.Random
import System.Threading
import System.DateTime

# System assemblies

clr.AddReference("System.Core")
clr.AddReference("System.Data")
clr.AddReference("System.Drawing")
...

#test.py
from references.syslibs import (Array, DataTable, OleDbConnection, OleDbDataAdapter,
                                 OleDbCommand, OleDbSchemaGuid)

def get_dict_from_data_table(dataTable):
    pass

2 module with 'import' and 'from ... import ...' as well as methods and classes:

from ... import ...
from ... import ...

def Generate(param, param1 ...):
    pass

3 module with 'import' and 'from ... import ...' statements which are used inside of methods and classes:

import clr
clr.AddReference("assembly")       

from ... import ...
...

def generate_(txt, param1, param2):
  from ... import ...
  from ... import ...
  from ... import ...

  if not cond(param1): res = "text"
  if not cond(param2): name = "default"

So what is the most pythonic way to import modules in python?

It really doesn't matter, so long as you don't from ... import * . The rest is all taste and getting around cyclic import issues. PEP 8 states that you should import at the top of the script, but even that isn't set in stone.

People have already commented on the major style issues (at the top of the script, etc), so I'll skip that.

For my imports, I usually have them ordered alphabetically by module name (regardless of whether it's 'import' or 'from ... import ...'. I split it into groups of: standard lib; third party modules (from pypi or other); internal modules.

import os
import system

import twisted
import zope

import mymodule_1
import mymodule_2

Python's "import" loads a Python module into its own namespace, so that you have to add the module name followed by a dot in front of references to any names from the imported module

import animals
animals.Elephant()

"from" loads a Python module into the current namespace, so that you can refer to it without the need to mention the module name again

from animals import Elephant
Elephant()

or

from animals import *
Elephant()

using from is good, (but using a wildcard import is discouraging). but if you have a big scaled project, importing from diffrent modules may cause naming confilicts. Like importing Elephant() function from two diffrent modules will cause problem (like using wildcard imports with * )

So, if you have a large scaled project where you import many diffrent things from other modules, it is better to use import and using imported things with module_name.your_class_or_function . Otherwise, use from notation...

Do not use from module import * . This will pollute the namespace and is highly frowned upon. However, you can import specific things using from; from module import something . This keeps the namespace clean. On larger projects if you use a wildcard you could be importing 2 foo or 2 bar into the same namespace.

PEP 8 says to have imports on separate lines. For instance:

import os
import sys
import yourmodule
from yourmodule import specific_stuff

One thing I do is alphabetize my imports into two groups. One is std/third party and the second is internal modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM