[英]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 namedspam.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.