Python ModuleNotFoundError: No module named 'patsy' when using ggplot

1.1k Views Asked by At

I am attempting to run the following code to plot the explained variance after applying PCA on my dataframe:

(ggplot(pcaDF, aes(x = "Principal Components", y = "expl_var")) + geom_line() + geom_point())

However, I keep on getting this error message:

--------------------------------------------------------------------------- ModuleNotFoundError                       Traceback (most recent call last) /var/folders/4q/z12sygps24zfmyncnf31fmdw0000gn/T/ipykernel_87587/3283535859.py in <module>
----> 1 from plotnine import *
      2 
      3 (ggplot(pcaDF, aes(x = "Principal Components", y = "expl_var")) + geom_line() + geom_point())

~/anaconda3/lib/python3.7/site-packages/plotnine/__init__.py in <module>
----> 1 from .qplot import qplot            # noqa: F401
      2 from .ggplot import ggplot, ggsave  # noqa: F401
      3 from .ggplot import save_as_pdf_pages  # noqa: F401
      4 from .watermark import watermark    # noqa: F401
      5 from .mapping import *              # noqa: F401,F403,E261

~/anaconda3/lib/python3.7/site-packages/plotnine/qplot.py in <module>
      5 import pandas.api.types as pdtypes
      6 import numpy as np
----> 7 from patsy.eval import EvalEnvironment
      8 
      9 from .ggplot import ggplot

ModuleNotFoundError: No module named 'patsy'

My machine is a Mac and I am using JupyterLab and Anaconda navigator.

I then installed patsy using the terminal by running the following command:

pip3 install patsy



Collecting patsy
  Downloading patsy-0.5.2-py2.py3-none-any.whl (233 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 233.7/233.7 kB 1.1 MB/s eta 0:00:00
Collecting numpy>=1.4
  Downloading numpy-1.23.3-cp310-cp310-macosx_10_9_x86_64.whl (18.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.1/18.1 MB 1.8 MB/s eta 0:00:00
Collecting six
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, numpy, patsy
Successfully installed numpy-1.23.3 patsy-0.5.2 six-1.16.0

I then restarted the kernel on jupyterlab but I am still getting the same error message above.

Please help!

1

There are 1 best solutions below

4
Ftagliacarne On

When you installed patsy were you in the conda environment? (maybe base but hopefully something else). If you weren't then type in your terminal

$ conda activate ENVNAME

And try again.

If you were, you can check if pip is pointing to the correct place by typing

$ which pip

If the string that is returned is under your environment then maybe jupyter is being launched from the wrong environment. If however, the version of pip is not in the current environment (e.g. /usr/local/bin/pip) install pip on conda using

$ conda install pip

Running which pip should now return a path that either points to your environment or something like ~/anaconda3/bin/pip.

Now you can install patsy just like before by typing

$ pip install patsy

And it should work.