【问题标题】:ImportError for existing module in PythonPython中现有模块的ImportError
【发布时间】:2017-12-21 08:02:51
【问题描述】:

我正在尝试将一系列模块导入到我的 Python 3.5 代码中。我使用以下代码导入:

# import packages for analysis and modeling
import pandas as pd  # data frame operations; use pandas 0.18
from pandas.tools.rplot import RPlot, TrellisGrid, GeomPoint, \
ScaleRandomColour  # trellis/lattice plotting
import numpy as np  # arrays and math functions
from scipy.stats import uniform  # for training-and-test split
import statsmodels.api as sm  # statistical models (including regression)
import statsmodels.formula.api as smf  # R-like model specification
import matplotlib.pyplot as plt  # 2D plotting 

当我使用此代码时,我收到以下错误:

ImportError                               Traceback (most recent call last)
/var/folders/zy/snhf2bh51v33ny6nf7fyr4wh0000gn/T/tmpdxMQ0Y.py in <module>()
      7 # import packages for analysis and modeling
      8 import pandas as pd  # data frame operations; use pandas 0.18
----> 9 from pandas.tools.rplot import RPlot, TrellisGrid, GeomPoint, \
     10     ScaleRandomColour  # trellis/lattice plotting
     11 import numpy as np  # arrays and math functions
ImportError: No module named 'pandas.tools.rplot' 

我尝试使用“pd”和写出“pandas”的代码。我通过手动输入import pandas as pd 确认安装了pandas,然后通过输入“pd”确认它的存在并收到以下消息:&lt;module 'pandas' from '/Users/me/Library/Enthought/Canopy/edm/envs/User/lib/python3.5/site-packages/pandas/__init__.py'&gt;

发生这种情况的原因是什么?

【问题讨论】:

  • 回溯中列出的代码与问题开头的代码不匹配。
  • 抱歉,我重新运行了原始代码并在编辑中将其复制粘贴到上面。

标签: python python-3.x packages python-import


【解决方案1】:

import 期间将其重命名为 as 并不意味着当您在稍后的导入语句中使用名称 pd 时,Python 将能够找到原始模块 (pandas)。 Python 将寻找一个名为 pd 的模块,但它不会找到。

由于pd 不对应于某些模块,而pandas 对应,您需要使用from pandas import tools 才能使其工作。

【讨论】:

  • 我都试过了,同样的错误发生了:Traceback (most recent call last): File "&lt;ipython-input-60-1f5a46b0c433&gt;", line 2, in &lt;module&gt; from pandas.tools.rplot import RPlot, TrellisGrid, GeomPoint, ScaleRandomColour # trellis/lattice plotting ImportError: No module named 'pandas.tools.rplot'
  • @Yehuda 你运行什么版本的熊猫?似乎该软件包不再可用,但是,您的原始问题已得到充分解决。
  • 我的包管理器说我运行的是 0.19.2-4,它说这是最新版本。
猜你喜欢
  • 2018-05-13
  • 1970-01-01
  • 2019-12-12
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多