Yehuda
Yehuda

Reputation: 1893

ImportError for existing module in Python

I'm trying to import a series of modules into my Python 3.5 code. I use the following code to import:

# 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 

When i use this code, I receive the following error:

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' 

I tried this code with "pd" and with "pandas" written out. I confirmed that pandas was installed by manually typing in import pandas as pd and then confirming its existence by typing in "pd" and receiving the following message: <module 'pandas' from '/Users/me/Library/Enthought/Canopy/edm/envs/User/lib/python3.5/site-packages/pandas/__init__.py'>

What is causing this to happen?

Upvotes: 1

Views: 546

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160377

Renaming it during import with as doesn't mean Python will be able to find the original module (pandas) when you use the name pd at a later import statement. Python will look for a module named pd which it will not find.

Since pd does not correspond to some module while pandas does, you'll need to use from pandas import tools in order to get it to work.

Upvotes: 4

Related Questions