日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python】import pandas时,报错 pandas Missing required dependencies ['numpy'] 原因分析

發布時間:2024/2/28 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】import pandas时,报错 pandas Missing required dependencies ['numpy'] 原因分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

結論:在需要 import pandas 時,同級目錄下不要有 unittest.py同名文件,否則,會與pandas自帶的unittest沖突,導致 import pandas 失敗。


錯誤使用場景復現

建立了一個 testpandas.py,同時,在同級目錄下建立了一個 unittest.py。
在 testpandas.py 中 import pandas, 運行發現報錯。


報錯信息

Traceback (most recent call last):File "C:\Python27\lib\site-packages\pandas\__init__.py", line 13, in <module>__import__(dependency)File "C:\Python27\lib\site-packages\numpy\__init__.py", line 187, in <module>from .testing import TesterFile "C:\Python27\lib\site-packages\numpy\testing\__init__.py", line 10, in <module>from unittest import TestCase ImportError: cannot import name TestCase Traceback (most recent call last):File "C:/Users/Bug/PycharmProjects/md-jobs-latest/app/mdjob/testpandas.py", line 8, in <module>import pandasFile "C:\Python27\lib\site-packages\pandas\__init__.py", line 20, in <module>"Missing required dependencies {0}".format(missing_dependencies)) ImportError: Missing required dependencies ['numpy']

原因分析

根據報錯,查看C:\Python27\lib\site-packages\numpy\testing\__init__.py源碼,如下:

"""Common test support for all numpy test scripts.This single module should provide all the common functionality for numpy tests in a single location, so that test scripts can just import it and work right away.""" from __future__ import division, absolute_import, print_functionfrom unittest import TestCasefrom ._private.utils import * from ._private import decorators as dec from ._private.nosetester import (run_module_suite, NoseTester as Tester)__all__ = _private.utils.__all__ + ['TestCase', 'run_module_suite']from numpy._pytesttester import PytestTester test = PytestTester(__name__) del PytestTester

可以看到,第 10 行的from unittest import TestCase與上圖testpandas.py同級目錄下的unittest(自己創建的)產生同名沖突。

也就是說,在unittest中找不到TestCase。也可以理解為,自己創建的unittest從名稱上污染了官方的unittest,錯誤的引用了自己建的unittest,所以報錯。


解決方式

在需要 import pandas 時,同級目錄下不要有 unittest.py的同名文件,否則,會與pandas自帶的unittest沖突,導致 import pandas 失敗。


拓展閱讀:from future import absolute_import 的作用

關于from __future__ import absolute_import的作用:
直觀地看就是說”加入絕對引入這個新特性”。說到絕對引入,當然就會想到相對引入。
那么什么是相對引入呢?比如說,你的包結構是這樣的:

pkg/ pkg/init.py pkg/main.py pkg/string.py

如果你在main.py中寫import string,那么在Python 2.4或之前, Python會先查找當前目錄下有沒有string.py, 若找到了,則引入該模塊,然后你在main.py中可以直接用string了。如果你是真的想用同目錄下的string.py那就好,但是如果你是想用系統自帶的標準string.py呢?那其實沒有什么好的簡潔的方式可以忽略掉同目錄的string.py而引入系統自帶的標準string.py。這時候你就需要from __future__ import absolute_import了。這樣,你就可以用import string來引入系統的標準string.py, 而用from pkg import string來引入當前目錄下的string.py了

總結

以上是生活随笔為你收集整理的【Python】import pandas时,报错 pandas Missing required dependencies ['numpy'] 原因分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。