[英]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代码,并找到了几种不同的方法来导入模块,或者更精确 - 何时导入:
Please find samples below. 请在下面找到样品。
#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
from ... import ...
from ... import ...
def Generate(param, param1 ...):
pass
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.