简体   繁体   English

在python中导入模块的最pythonic方法是什么

[英]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? 任何人都可以建议我在python中导入模块的最pythonic方式是什么? 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: 让我解释一下 - 我已经阅读了很多python代码,并找到了几种不同的方法来导入模块,或者更精确 - 何时导入:

  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. 例如,在我们的项目中,我们将名为'references'的级别分开,因此它包含'system.py'(包含对所有系统库的引用),'platform.py'(包含对所有平台库的引用)等模块,'devexpress.py' '(包含对所有devexpress库的引用)等。 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): 1个示例导入模块 - 只有'import'和'from ... import ...'语句(没有任何方法或类):

#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: 2个模块带有'import'和'from ... import ...'以及方法和类:

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: 3模块带有'import'和'from ... import ...'语句,这些语句在方法和类中使用:

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? 那么在python中导入模块的最pythonic方法是什么?

It really doesn't matter, so long as you don't from ... import * . 真的没关系,只要你不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. PEP 8指出你应该在脚本的顶部导入,但即使这样也不是一成不变的。

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'还是'from ... import ...'。我将它分成以下组:标准库;第三方模块(来自pypi或其他);内部模块。

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 Python的“import”将Python模块加载到它自己的命名空间中,因此您必须在引用模块中的任何名称的引用前添加模块名称后跟一个点

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”将Python模块加载到当前名称空间中,以便您可以引用它而无需再次提及模块名称

from animals import Elephant
Elephant()

or 要么

from animals import *
Elephant()

using from is good, (but using a wildcard import is discouraging). 使用from很好,(但使用通配符导入是令人沮丧的)。 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 * ) 像从两个不同的模块导入Elephant()函数会导致问题(比如使用带*的通配符导入)

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 . 因此,如果您有一个大型缩放项目,从其他模块导入许多不同的东西,最好使用import并使用module_name.your_class_or_function导入的东西。 Otherwise, use from notation... 否则,请使用符号...

Do not use from module import * . 不要使用from module import * This will pollute the namespace and is highly frowned upon. 这将污染命名空间,并且非常不满意。 However, you can import specific things using from; 但是,您可以使用from导入特定的东西; from module import something . 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. 在较大的项目中,如果使用通配符,则可以将2 foo或2 bar导入到同一名称空间中。

PEP 8 says to have imports on separate lines. PEP 8表示在不同的生产线上进口。 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. 一个是标准/第三方,第二个是内部模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将“兄弟”模块相互导入的最 Pythonic 的方法是什么? - What is the most pythonic way to import 'sibling' modules into one another? 我是否应该始终使用最pythonic的方式导入模块? - Should I always use the most pythonic way to import modules? Python 2:使用字符串解释进行枚举的最优雅/ pythonic方式是什么? - Python 2: What is the most elegant/pythonic way of doing a enum with string interpretations? 构造多模块python程序的最pythonic方法是什么? - What is the most pythonic way to structure a multi-module python program? Pythonic从包中导入模块的方法 - Pythonic way to import modules from packages 对于具有指定编码的多个模块和多个处理程序,最具pythonic的日志记录方式是什么? - What is the most pythonic way of logging for multiple modules and multiple handlers with specified encoding? 在python库中导入模块的正确方法是什么? - What is the correct way to import modules in python library? 确定字节序的最Pythonic方法是什么? - What's the most Pythonic way of determining endianness? 处理标准输入的最Python方式是什么? - What is the most Pythonic way of handling standard input? 构建此词典的最pythonic方法是什么? - What's the most pythonic way to build this dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM