kensaii
kensaii

Reputation: 314

numpy cannot call function library directly

I'm using python 2.7 on ubuntu 16.04. As described in the code below, I can't use any function from the np.matlib, but after I import, then it can be used. Is there any way to troubleshoot the problem? Thanks in advance!

$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np 
>>> a = np.matlib.repmat([1,1],1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'matlib'
>>> import numpy.matlib as npm
>>> a = npm.repmat([1,1],1,2)
>>> print a
[[1 1 1 1]]
>>>

I think this is a library clash, and if so, how do I know which clashes against which?

Upvotes: 0

Views: 230

Answers (2)

user2357112
user2357112

Reputation: 281330

The Python import system does not automatically load submodules of a package when a package is imported. NumPy's __init__.py does automatically load most NumPy submodules on a plain import numpy, but numpy.matlib is not included.

Until some code somewhere in the program explicitly imports numpy.matlib, numpy.matlib will not exist, and its contents will not be accessible.

Upvotes: 1

sam
sam

Reputation: 376

import numpy.matlib as npm

This does not introduce the name numpy.matlib into the namespace; it only introduces the name npm. Python 2.7 doc reference.

If you want the module to be available through both numpy.matlib and npm, you can just define it that way, i.e. npm = numpy.matlib.

Upvotes: 0

Related Questions