通常,使用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/