1

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!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
sums22
  • 1,793
  • 3
  • 13
  • 25

1 Answers1

1

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.

Ftagliacarne
  • 675
  • 8
  • 16
  • Thank you for your answer. I ran these commands, running 'which pip' returns /usr/local/bin/pip. What does this mean? – sums22 Sep 27 '22 at 13:03
  • It means that you're not using the conda pip. But you can see from the error that your python path is `~/anaconda3/lib/python3.7` meaning that you are using conda. Maybe try installing pip with `conda install pip` – Ftagliacarne Sep 29 '22 at 13:01
  • That is very helpful, thank you. I ran 'conda install pip' and then reran the command 'which pip' and now it returns /Users/myUserName/anaconda3/bin/pip. I then reran 'pip install patsy' and now the import works. Thank you so much! – sums22 Sep 30 '22 at 10:31
  • 1
    If you could include this additional info about how to use the correct conda pip and then run the patsy install again so it is a complete answer. I will then proceed to accept your answer :) – sums22 Sep 30 '22 at 10:36