在python中导入模块的指南

在python中导入模块的指南,python,python-import,Python,Python Import,我知道在python中导入模块有很多问题,但我的问题似乎有所不同 >>> from os.path import basename >>> basename('scripts/cy.py') 'cy.py' 我试图理解何时必须导入整个模块,而不是何时必须导入模块中的特定条目。这两种方法似乎只有一种有效 >>> from os.path import basename >>> basename('scripts/cy.py'

我知道在python中导入模块有很多问题,但我的问题似乎有所不同

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
我试图理解何时必须导入整个模块,而不是何时必须导入模块中的特定条目。这两种方法似乎只有一种有效

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
例如,如果我想使用basename,那么导入os.path不会起作用

>>> import os.path
>>> basename('scripts/cy.py')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'basename' is not defined
>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
另一方面,如果我想使用shutil.copyfile,那么从shutil导入copyfile是行不通的

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
>>> 
>>> from shutil import copyfile
>>> 
>>> shutil.copyfile('emma','newemma')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'shutil' is not defined
我唯一能做到这一点的方法就是通过实验。 是否有一些避免实验的指导原则?

如果导入

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
import os.path
然后您必须使用完整的名称空间
os.path

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
os.path.basename()
xxx.basename()
如果使用
从导入

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
from shutil import copyfile
那么您就不必使用完整的名称空间
shutil

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
copyfile(...)
就这些

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'

如果将
用作

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
import os.path as xxx
from os.path import basename as xxx
然后您必须使用
xxx
而不是
os.path

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
os.path.basename()
xxx.basename()

如果使用
from
as

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
import os.path as xxx
from os.path import basename as xxx
然后您必须使用
xxx
而不是
basename

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
xxx()
如果你进口

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
import os.path
然后您必须使用完整的名称空间
os.path

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
os.path.basename()
xxx.basename()
如果使用
从导入

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
from shutil import copyfile
那么您就不必使用完整的名称空间
shutil

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
copyfile(...)
就这些

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'

如果将
用作

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
import os.path as xxx
from os.path import basename as xxx
然后您必须使用
xxx
而不是
os.path

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
os.path.basename()
xxx.basename()

如果使用
from
as

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
import os.path as xxx
from os.path import basename as xxx
然后您必须使用
xxx
而不是
basename

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
xxx()

您可以使用表单模块导入*

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
from time import *
sleep(2)
允许您调用其子模块,而不是:

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
from time import sleep
sleep(2)
或:

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
这将导入包的每个子模块
您可以使用表单模块导入*

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
from time import *
sleep(2)
允许您调用其子模块,而不是:

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
from time import sleep
sleep(2)
或:

>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
这将导入包的每个子模块

归结起来就是:您只能引用在
import
语句中使用的任何字符串,因此
import a.b;a、 b.c()
是有效的,a.b导入c的
也是有效的;c()
,但这两个是无效的:
导入a.b;c();c()。解释器打开它,读取并解释它,然后创建一个指向模块及其内容的“变量”。很好,我不能再编辑我的注释了,但最后一个示例应该是a.b import c中的
;a、 b.c()
。归结起来就是:您只能引用在
import
语句中使用的任何字符串,因此
import a.b;a、 b.c()
是有效的,a.b导入c的
也是有效的;c()
,但这两个是无效的:
导入a.b;c();c()。解释器打开它,读取并解释它,然后创建一个指向模块及其内容的“变量”。很好,我不能再编辑我的注释了,但最后一个示例应该是a.b import c中的
;a、 b.c()
xxx.basename()
xxx.basename()
不要这样做<代码>来自。。。import*
将名称空间搞得一团糟,风格也不好。这正是他所要求的,即使我在文档中共享了一个链接,指出了所有的副作用,如果模块设计师做了一个all,也不会搞得一团糟,那么为什么要投反对票呢T@jwodderDon不能这样做<代码>来自。。。import*
将名称空间搞得一团糟,风格也不好。这正是他所要求的,即使我在文档中共享了一个链接,指出了所有的副作用,如果模块设计师做了一个all,也不会搞得一团糟,那么为什么要投反对票呢T@jwodder
>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'