通常,使用import numpy as np
导入模块 numpy。
是否有通用的命名约定?
其他模块呢,特别是像 scipy
、sympy
和 pylab
这样的科学计算模块,或者像 scipy.sparse< 这样的子模块
.
最佳答案
SciPy 建议在 its documentation 中import scipy as sp
,尽管我个人认为这没什么用,因为它只允许您访问重新导出的 NumPy 功能,而不是 SciPy 添加的任何内容。我发现自己更频繁地执行 import scipy.sparse as sp
,但后来我大量使用该模块。还有
import matplotlib as mpl
import matplotlib.pyplot as plt
import networkx as nx
当您开始使用更多库时,您可能会遇到更多此类问题。这些速记没有注册表或任何东西,您可以根据需要自由发明新的速记。除了 import lln as library_with_a_long_name
显然不会经常发生之外,也没有通用约定。
除了这些简写之外,Python 2.x 程序员还习惯于做类似的事情
# Try to import the C implementation of StringIO; if that doesn't work
# (e.g. in IronPython or Jython), import the pure Python version.
# Make sure the imported module is called StringIO locally.
try:
import cStringIO as StringIO
except ImportError:
import StringIO
Python 3.x 结束了这种情况,因为它不再提供 StringIO
、pickle
等的部分 C 实现。
关于python - 'import ... as' 的约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14960336/