简体   繁体   English

错误:AttributeError:模块“numpy”没有属性“sqrt”

[英]Error: AttributeError: module 'numpy' has no attribute 'sqrt'

import numpy as np
np. sqrt(3)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-029a7ac454da> in <module>()
      1 import numpy as np
----> 2 np. sqrt(3)

AttributeError: module 'numpy' has no attribute 'sqrt'

I know it's basic but please guide.我知道这是基本的,但请指导。 I am new to python 3.我是 python 3 的新用户。

Make sure that you don't have a file named numpy.py in your workspace.确保您的工作区中没有名为numpy.py的文件。

(test-py38) gino:test$ touch numpy.py
(test-py38) gino:test$ ll
total 0
-rw-r--r-- 1 gino gino 0  6月 10 12:30 numpy.py

(test-py38) gino:test$ ipython
Python 3.8.3 (default, May 14 2020, 20:11:43) 
...

In [1]: import numpy as np 

In [2]: np. sqrt(3)                                                                                                     
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-54486447ed79> in <module>
----> 1 np. sqrt(3)

AttributeError: module 'numpy' has no attribute 'sqrt'

You must not have files named after Python packages because import will import your file instead of the actual package. You need to rename them to something else.不能有以 Python 包命名的文件,因为import将导入你的文件而不是实际的 package。你需要将它们重命名为其他名称。

See the Module Search Path docs for info how Python imports stuff:有关 Python 如何导入内容的信息,请参阅模块搜索路径文档:

When a module named spam is imported, the interpreter first searches for a built-in module with that name.当导入名为spam的模块时,解释器首先搜索具有该名称的内置模块。 If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path .如果找不到,它会在变量sys.path给出的目录列表中搜索名为spam.py的文件。

  • The directory containing the input script (or the current directory when no file is specified).包含输入脚本的目录(或未指定文件时的当前目录)。
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH). PYTHONPATH(目录名称列表,语法与 shell 变量 PATH 相同)。
  • The installation-dependent default.安装相关的默认值。

You could also check that it's importing the correct package by printing __path__ :您还可以通过打印__path__

(test-py38) gino:test$ ipython
Python 3.8.3 (default, May 14 2020, 20:11:43) 
...

In [1]: import numpy as np

In [2]: np.__path__                                                                                                     
Out[2]: ['/path/to/.venvs/test-py38/lib/python3.8/site-packages/numpy']

In [4]: np.  sqrt(3)     # The spaces don't matter                                                                      
Out[4]: 1.7320508075688772

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

相关问题 Numpy 抛出错误:AttributeError: module 'numpy' has no attribute 'loadtxt'? - Numpy throws a error : AttributeError: module 'numpy' has no attribute 'loadtxt '? numpy标准偏差AttributeError:“浮动”对象没有属性“ sqrt” - Numpy Standard Deviation AttributeError: 'Float' object has no attribute 'sqrt' 我面临这个错误;; AttributeError:模块“numpy”没有属性“corroef” - I face this error;; AttributeError: module 'numpy' has no attribute 'corroef' AttributeError: 模块“numpy”没有属性“array” - AttributeError: module 'numpy' has no attribute 'array pyInstaller AttributeError:模块“numpy”没有属性“_NoValue” - pyInstaller AttributeError: module 'numpy' has no attribute '_NoValue' AttributeError:模块'numpy'没有属性'core' - AttributeError: module 'numpy' has no attribute 'core' AttributeError: 模块 &#39;numpy&#39; 没有属性 &#39;cost&#39; - AttributeError: module 'numpy' has no attribute 'cost' AttributeError:模块“numpy”没有属性“__version__” - AttributeError: module 'numpy' has no attribute '__version__' AttributeError:模块'numpy'没有属性'typeDict' - AttributeError: module 'numpy' has no attribute 'typeDict' AttributeError:模块'numpy'没有属性'float' - AttributeError: module 'numpy' has no attribute 'float'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM