简体   繁体   English

导入模块时出错

[英]Error when importing modules

I'm getting the following error when importing modules in python. 在python中导入模块时出现以下错误。 I'm using jupyter notebook (python 2). 我正在使用jupyter笔记本(python 2)。 I've searched through the internet but still can't quite figure out why. 我已经在互联网上进行搜索,但仍然不知道为什么。 Any help would be so much appreciated. 任何帮助将不胜感激。

Here's the code: 这是代码:

import numpy as np
from pandas import Series,DataFrame
import pandas as pd
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-e4e9959b303a> in <module>()
----> 1 import numpy as np
  2 from pandas import Series,DataFrame
  3 import pandas as pd

 /Users/...filepath.../Python/data_analysis/numpy.pyc in <module>()
      17 
      18 import numpy as np
 ---> 19 from numpy.random import randn
      20 
      21 

      ImportError: No module named random

I've tried adding import random to the above code (before the other modules) and it still gives the same error. 我试过将import random添加到上述代码中(在其他模块之前),它仍然给出相同的错误。 Could this be due to the version of gfortran on my system? 这可能是由于我系统上的gfortran版本导致的吗? I have version 4.9.2 我有4.9.2版

Since I dont have complete code, just tried using import statements. 由于我没有完整的代码,因此请尝试使用import语句。

If we use np as per @John 如果我们按照@John使用np
import numpy as np
from np.random import randn

I am getting 我正进入(状态
from np.random import randn
ImportError: No module named np.random

I am not getting any error if I import randn from numpy.random 我没有得到任何错误,如果我输入randnnumpy.random

import numpy as np
from numpy.random import randn
print "randn1= ", randn()

from numpy.random import rand
print "rand1= ", rand()

Its working for me with output as below, 它为我工作,输出如下,

randn1=  0.147667079884  
rand1=  0.243935746205  

You can also try to use np.random.randn() and np.random.rand() directly. 您也可以尝试直接使用np.random.randn()np.random.rand()

import numpy as np
print "randn2= ", np.random.randn()
print "rand2= ", np.random.rand()

I get : 我得到:
randn2= -0.22571513741
rand2= 0.486507681046

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM