Score:0

Jupyter NB fails to import packages that work well on the Ubuntu cmd line

yt flag

[I started using Linux (Ubuntu) very recently to be able to run SageMath without problems - i.e. no idea!]

The Jupyter notebook in Ubuntu allows running SageMath, Rstats and Python all at the same time, and it works perfectly. Currently I'm using Ubuntu on WSL, and running python3. I have tried to implement the suggestions in multiple similar questions on this site and others without success.

When I use the command line I have no issues importing pandas and statsmodels and working with them (see example below).

However, if I try to run the same chunk of code from the Jupyter NB, I get the following message:

import statsmodels.api as sm              

import statsmodels.stats.stattools as stools

import statsmodels.stats as stats

from   statsmodels.graphics.regressionplots import *

from   statsmodels.sandbox.regression.predstd import wls_prediction_std

from   statsmodels.formula.api import ols

---------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/tmp/ipykernel_1044/2072660261.py in <module>
----> 1 import statsmodels.api as sm
      2 import statsmodels.stats.stattools as stools
      3 import statsmodels.stats as stats
      4 from   statsmodels.graphics.regressionplots import *
      5 from   statsmodels.sandbox.regression.predstd import wls_prediction_std

ModuleNotFoundError: No module named 'statsmodels'

I tried restarting the PC. Why doesn't Jupyter NB recognize statsmodels, and how can I fix this?


Example (from the command line in Ubuntu):

user@antoni:~$ python3
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
t pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
import statsmodels.stats.stattools as stools
import statsmodels.stats as stats
from   statsmodels.graphics.regressionplots import *
from   statsmodels.sandbox.regression.predstd import wls_prediction_std
from   statsmodels.formula.api import ols
import io
import requests

url = "https://raw.githubusercontent.com/RInterested/datasets/gh-pages/mtcars.csv"
contents = requests.get(url).content
mtcars = pd.read_csv(io.StringIO(contents.decode('utf-8')))
mtcars['wt_square']=mtcars['wt']**2
model = ols('mpg ~ wt + wt_square', data=mtcars).fit()
print(model.summary())>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> import statsmodels.api as sm
>>> import statsmodels.stats.stattools as stools
>>> import statsmodels.stats as stats
>>> from   statsmodels.graphics.regressionplots import *
>>> from   statsmodels.sandbox.regression.predstd import wls_prediction_std
>>> from   statsmodels.formula.api import ols
>>> import io
>>> import requests
>>>
>>> url = "https://raw.githubusercontent.com/RInterested/datasets/gh-pages/mtcars.csv"
>>> contents = requests.get(url).content
>>> mtcars = pd.read_csv(io.StringIO(contents.decode('utf-8')))
>>> mtcars['wt_square']=mtcars['wt']**2
>>> model = ols('mpg ~ wt + wt_square', data=mtcars).fit()
>>> print(model.summary())
                            OLS Regression Results
==============================================================================
Dep. Variable:                    mpg   R-squared:                       0.819
Model:                            OLS   Adj. R-squared:                  0.807
Method:                 Least Squares   F-statistic:                     65.64
Date:                Sat, 20 May 2023   Prob (F-statistic):           1.71e-11
Time:                        13:46:59   Log-Likelihood:                -75.024
No. Observations:                  32   AIC:                             156.0
Df Residuals:                      29   BIC:                             160.4
Df Model:                           2
Covariance Type:            nonrobust
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
---------------------------------------------
Intercept     49.9308      4.211     11.856      0.000      41.318      58.544
wt           -13.3803      2.514     -5.322      0.000     -18.522      -8.239
wt_square      1.1711      0.359      3.258      0.003       0.436       1.906
==============================================================================
Omnibus:                        4.261   Durbin-Watson:                   1.732
Prob(Omnibus):                  0.119   Jarque-Bera (JB):                3.788
Skew:                           0.832   Prob(JB):                        0.150
Kurtosis:                       2.731   Cond. No.                         142.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Antoni Parellada avatar
yt flag
In case it helps someone else, this seems to be working now. I don't know what did the trick, but I ran `$ sudo apt install python3-pip python3-dev`, `$ sudo -H pip3 install --upgrade pip` and `$ sudo -H pip3 install virtualenv`
Antoni Parellada avatar
yt flag
It is possible that the lines of code above did absolutely nothing. I have noticed that if I start the NB with `sage -n` statsmodels fails to import; however, if I start the NB with `jupyter notebook` everything is fine.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.