【发布时间】:2012-04-16 22:43:17
【问题描述】:
我以两种不同的方式导入了numpy 库。第一次使用from numpy.random import mtrand,第二次使用sys.path。
但是,这两个模块导入的输出完全不同:
>>> from numpy.random import mtrand
>>> dir(mtrand)
['RandomState', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__', '_rand', 'beta', 'binomial', 'bytes', 'chisquare', 'dirichlet', 'exponential', 'f', 'gamma', 'geometric', 'get_state', 'gumbel', 'hypergeometric', 'laplace', 'logistic', 'lognormal', 'logseries', 'multinomial', 'multivariate_normal', 'negative_binomial', 'noncentral_chisquare', 'noncentral_f', 'normal', 'np', 'pareto', 'permutation', 'poisson', 'power', 'rand', 'randint', 'randn', 'random_integers', 'random_sample', 'rayleigh', 'seed', 'set_state', 'shuffle', 'standard_cauchy', 'standard_exponential', 'standard_gamma', 'standard_normal', 'standard_t', 'triangular', 'uniform', 'vonmises', 'wald', 'weibull', 'zipf']
第二个:
>>> sys.path.insert(0, '/usr/lib/pymodules/python2.7/numpy/random')
>>> import mtrand
>>> dir(mtrand)
['__builtins__', '__doc__', '__file__', '__name__', '__package__']
这种行为怎么可能?
编辑:
- 这两个测试在不同的 python 进程中执行。
- 弄乱系统路径是愚蠢的,我知道。但这不适用于普通程序,而是用于自动完成。我当然不想导入整个 numpy 包。我只想制作一个
dir(mtrand)
【问题讨论】:
-
这让我觉得自动完成的错误方法。 (Always include your actual aims in the question right from the beginning.) 你不能在不导入 NumPy 的情况下导入
mtrand- 对我来说,你的第二种方法也“有效”,但就像正常导入一样,将所有 NumPy 绘制到sys.modules中。 -
与其自己编写代码,不如查看标准库中的
rlcompleter、此模块的IPython's enhanced version 和rope's 自动补全。后者是最先进的。 -
rlcompleter 和 IPython 非常简单。 Rope 是唯一真正的自动补全。但这对我从来没有用过——所以我决定做一些更好的事情(所有其他解决方案都是简单的 -> python-omnicomplete,或者不是免费的 -> pycharm)。它已经工作得很好,但内置函数仍然是个问题,因为它们无法被评估。