0

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?

3
  • Can't reproduce. Is that the actual order in which you executed those statements? I bet you didn't actually execute import numpy.matlib as npm before a = np.matlib.repmat([1,1],1,2). Commented Mar 7, 2018 at 2:49
  • @user2357112 you are absolutely right. sorry. just update to reflect the change Commented Mar 7, 2018 at 2:53
  • This is just one of the sub packages that requires its own import. Commented Mar 7, 2018 at 6:18

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.