Jupyter not recognizing installed 'statsmodels' package - python

I have a Jupyter notebook running inside an Anaconda virtual environment that won't recognize the statsmodels package. I've used both pip and conda, and I've reinstalled the whole environment several times in between restarting my computer. As you can see in this link, both pip and conda show that it is installed. However, I continue to get this error:
---> 43 import statsmodels.api as sm
ModuleNotFoundError: No module named 'statsmodels'
At first I thought it was the kernel, but that wouldn't explain why every other package I've installed is recognized properly.
The only thing I haven't done is tried it on another computer (I'm running Windows 10 x64). I just want to know if anyone can see something that I'm missing before I do so.
Thanks!
Edit:
I've tried the following commands to install it:
pip install statsmodels
conda install -c conda-forge statsmodels
conda install statsmodels
I've tried using each command individually, making sure to uninstall the previous installation. I've tried using only two at a time, with the same methodology. I've tried every permutation of the above installations, with a computer restart in between. I don't think the problem is that it's not installing. I have no clue why but Jupyter notebooks just isn't recognizing this one package.
I'm running all of my commands from an Anaconda command prompt, and I make sure to activate my focal virtual environment. I also run Jupyter lab by typing it directly into the command prompt and hitting enter (I make sure I've activated my environment).
I'll try running it on a different OS and update.

Thanks duffymo! My notebook wasn't using the correct kernel. I thought all I needed to do was run Jupyter lab from my virtual environment, but it needs to be purposefully added. I followed the directions shown here to add my conda environment to Jupyter lab:
How to add conda environment to jupyter lab

Related

Having trouble installing Pytorch on Windows

I have tried install pytorch with pip but there is no package fit for my environment. I use Python 3.11.2. Then I downloaded anaconda and installed pytorch successfully. However, when I imported torch in python, it said there is no such module.
I followed many tutorials by restart my laptop, created conda enviroment and install pytorch there. Nothing worked.
Anyone having similar problems?
That's not clear for your problem. I think you may show the process of how you installed pytorch by conda.
You can use conda list pip list to show your package installed.
The problem in anaconda may be you install pytorch in your specific env, but when you open your ide (i.e. vscode, pycharm) you choose the base env. You may try run your code in your terminal by conda activate xxxx then python3 then import torch.

conda kernels and conda modules not found in jupyter notebooks when added, but otherwise existant?

There are many questions like this (e.g. Conda environments not showing up in Jupyter Notebook, Python: modul not found after Anaconda installation, https://askubuntu.com/questions/1271137/importing-anaconda-libraries-not-working). I have tried their solutions but to no avail.
In short, I have conda installed. I have installed jupyterlabs and nb_conda_kernels in my base environment. When I create a new conda env via a file, the environment is not listed in jupyter automatically. I can add it via
python -m ipykernel install --user --name <env-name>
but then after I restart jupyter and switch kernels, when I try to import a python module from it, it says module not found.
What do I need to do to fix this? I do not want to add jupyterlabs to every env.
NOTE:
when activating the env in the terminal the path is correct, e.g. /opt/anaconda/anaconda3/envs/<env-name>/bin
when using the env in the terminal, the pack is there.
when using the kernel in Jupyter, the module is not there.

Can't install python packages for a conda environment

I could use some clarification regarding anaconda envs and the installed packages. I have just began using environments other than the base to keep my installations clean.
I just deleted my anaconda3 folder, installed it from scratch, made a new environment and tried to run a jupyter-notebook.
When reading excel via pandas I get the error Missing optional dependency 'xlrd'
prompting me to install xlrd via conda or pip.
So I open the terminal, activate the env I’m working in and install it using conda. I close the JN from the terminal, reopen it and run the same code. The problem persists.
I repeat the procedure, this time indicating the name of the env at the end. Problem persists.
I checked which python in the terminal, it is indeed the anaconda3/envs/newenv/bin/python
I do the same (in the new env) using pip. I use the command .../anaconda3/envs/newenv/bin/python -m pip install xlrd. I get the message Requirement already satisfied: xlrd in ./anaconda3/envs/newenv/lib/python3.7/site-packages (1.2.0).
I even tried installing the package inside the notebook using !conda install xlrd, still I get the same error.
Finally, I open Jupyter via the base env, and the package works there perfectly.
I have no idea why I can’t install this under the newenv, which was the point of having local envs after all.
Thanks heaps for your help!
You need to install the kernel in Jupyter to be able to use it.
jupyter kernelspec list
That command will give you the list of kernels you have. I am assuming it only shows you Python3
You will now need to install a kernel. Remember to do this while inside your virtual environment
python3 -m pip install ipykernel
python3 -m ipykernel install --user --name <your-new-kernel-name>
You should now see this in Jupyter notebook. Select the kernel in Jupyter and you should be good to go.
Another thing you may want to try is to install Jupyter while inside your virtual environment. While inside your virtual environment, you could do:
python3 -m pip install jupyter lab
and then while still inside your virtual environment, run jupyter after checking which jupyter. It should solve your problem as well.

Jupyter can't find keras' module

I have installed Tensorflow and Keras by Anaconda (on Windows 10), I have created an environment where I am using Python 3.5.2 (the original one in Anaconda was Python 3.6).
When I try to execute import keras as ks, I get ModuleNotFoundError: No module named 'keras'.
I have tried to solve this issue by sys.path.append(C:\\Users\\ ... \\Anaconda3\\python.exe)
with both notebook and console, but I continue to get the same error.
How could I solve this issue?
Please try the following:
Run these in the jupyter notebook cell:
import sys
sys.path
sys.executable
It may not be pointing to your virtual environment but to the root
The fix is to install the jupyter notebook from inside your virtual environment
$ . your_env/bin/activate
(your_env)$ python -m pip install jupyter
Now you can import tensorflow or keras
Jupyter uses iPython under the hood, for python. So when you install Jupyter, it will also install iPython. There was one issue when I installed keras and Jupyter: I already have iPython installed in my root Anaconda environment. This is the output after I install Jupyter and keras:
In [2]: import sys; sys.path
Out[2]:
['/home/user/anaconda3/bin',
'/home/user/anaconda3/lib/python36.zip',
'/home/user/anaconda3/lib/python3.6',
'/home/user/.ipython']
Notice that even though I am inside my conda environment, it still looks for libraries in my root conda environment. And of course keras isn't there.
The step to fix is simply re-activate my environment, with:
source deactivate && source activate [my_env]
Then I am using a correct ipython:
Out[2]:
['/home/user/anaconda3/envs/ml3/bin',
'/home/user/anaconda3/envs/ml3/lib/python36.zip',
'/home/user/anaconda3/envs/ml3/lib/python3.6',
'/home/user/.ipython']
(Not an answer but some troubleshooting hints)
sys.path is not the path to your Python executable, but the path to the libraries.
Check where Keras is installed and check your sys.path. How exactly did you install Keras?
Try to execute the same command from the Python interpreter. Do you have the same issue?
How did you install Jupiter, is the sys.path visible from there the same as sys.path visible from your Python interpreter?
Do Jupiter and Keras use the same version of Python?
You might try to uninstall Jupiter and install it again, and hope that the new installation picks up the packages which are already installed. What could happen is that you have more than one Python installation and different libraries being installed to the different places. sys.path, when requested from different environments, might give you a hint if that's true.
The kernel in console and jupyter are not necessarily the same, and the problem might be that you are not on python 3.5.
python --version
should tell you what is running in the console, and in jupyter you should see it as a choice on starting a new notebook. For me, the information in
Using both Python 2.x and Python 3.x in IPython Notebook
was very helpful.
I have realized that I had two different Jupyter's directories, so I have manually deleted one on them. Finally, I have reinstalled Anaconda. Now Keras works properly.
If you are a windows/mac user who are working on Jupyter notebook “pip install keras” doesn't help you .Try the below steps.It was solved for me
1. In command prompt navigate to the “site packages” directory of your anaconda installed.
2. Now use “conda install tensorflow” and after “conda install keras”
3. Re-start your Jupyter notebook and run the packages.
Acually, I did this command pip install keras and sudo -H pip3 install keras and pip3 install keras. None of them worked. I added the following command and everything worked like a charm:
pip install Keras. Yes a capital 'K'
Here's how I solved this problem.
First, the diagnosis. When I run which python in a terminal window on my Mac (the same terminal I used to launch jupyter, I get /Users/myusername/.conda/envs/myenvname/bin/python, but when I run the same command from a terminal within Jupyter, I get /usr/bin/python. So Jupyter isn't using the correct python executable; the version it's using doesn't have any of my packages installed.
But which jupyter returns /usr/bin/jupyter; it's using a version of jupyter that isn't coming from within my conda environment. I ran conda install jupyter and now which jupyter returns /Users/myusername/.conda/envs/myenvname/bin/jupyter (for some reason I had to restart the terminal window for this to take effect.) Then if I relaunch jupyter notebook, the notebook is using the correct version of Python and I have access to all my installed conda packages. 👍
I had a similar problem.
I added the Conda environment as a new kernel.
First, install ipykernel:
conda install ipykernel
Next, create the kernet:
python -m ipykernel install --user --name tf-gpu --display-name "TensorFlow-GPU"
Now, when you run your notebook, change the kernel to the new one, to "TensorFlow-GPU" in this example.

How to install Matplotlib for anaconda 1.9.1 and Python 3.3.4?

I am configuring Anaconda 1.9.1 together with Python 3.3.4 and I am unable to setup Matplotlib for anaconda environment when I try to add package using Pycharm. I also tried to install from Matplotlib.exe file which I downloaded from its website. I can not change the installation directory in that case. I would like to know that is there a way to tackle this issue.
If you're using anaconda, your default environment is Python 2.7. You need to create a new environment and install matplotlib in there.
In a command prompt, do the following (saying yes to the questions):
conda create --name mpl33 python=3.3 matplotlib ipython-notebook
activate mpl33
ipython notebook
You should be able to import matplotlib when the notebook server comes up.
The first command simultaneously creates the environment and install
the listed packages.
The second command activates the new environment by prepending its location to the system path
The third command just starts the ipython notebook so that you can test out everything
I don't know how pycharm works, but my guess is that you'll have to tell it to look for the right python that you want to use. In this case it'll be something like: C:/Users//anaconda/envs/mpl33. In any case, the command prompt should display the path when you activate the environment.
Once you've activated your environment, you can install more packages like this:
conda install pandas=0.12
conda install pyodbc statsmodels
You can specific version numbers of packages like the first command or simply accept the latest available version (default)
Assuming you've already installed a 3.x python env in anaconda, this one line should do the trick:
conda install matplotlib -n name
where name is the name you previously gave to your python 3 anaconda env. If you're not sure of the name you gave it, it will be the name of a subdir in the Anaconda\envs directory.
Background: I recently went through the same trouble with matplotlib not getting installed by default by anaconda when I added a full python 3 env, even though it's meant to. The above line solved it for me; it gave me the following warnings so it seems likely that the two different available versions caused it to initially install neither. However it allowed me to choose the one I wanted, and then everything worked great.
Warning: 2 possible package resolutions:
[u'dateutil-2.1-py33_2.tar.bz2', u'matplotlib-1.3.1-np18py33_1.tar.bz2', u'numpy-1.8.0-py33_0.tar.bz2', u'pyparsing-2.0.1-py33_0.tar.bz2', u'pyside-1.2.1-py33_0.tar.bz2', u'python-3.3.5-0.tar.bz2', u'pytz-2013b-py33_0.tar.bz2', u'six-1.6.1-py33_0.tar.bz2']
[u'dateutil-2.1-py33_2.tar.bz2', u'matplotlib-1.3.1-np17py33_1.tar.bz2', u'numpy-1.7.1-py33_3.tar.bz2', u'pyparsing-1.5.6-py33_0.tar.bz2', u'pyside-1.2.1-py33_0.tar.bz2', u'python-3.3.5-0.tar.bz2', u'pytz-2013b-py33_0.tar.bz2', u'six-1.6.1-py33_0.tar.bz2'
]
conda install -c conda-forge matplotlib

Categories