(Jupyter Notebook) ModuleNotFoundError: No module named 'pandas' - python

In my terminal, I ran:
pip install pandas
pip3 install pandas
Installation seemed to go well. When I write some code in a file and execute it in my terminal (prompting 'python filename.py' or 'python3 filename.py'), the pandas library can be imported and used without a problem. However, when using Jupyter Lab and Jupyter Notebook, and I get this error when trying to import pandas:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-38d4b0363d82> in <module>
----> 1 import pandas
ModuleNotFoundError: No module named 'pandas'
It seems like Jupyter Notebook does not recognize this library. Very confused as of why and what I should do. FYI reinstalling anaconda did not help, and I am using 'pip' and 'pip3' to install libraries.

I had a similar problem. Your best bet is to install your packages direct from Jupyter notebook, then you can be sure that the packages are being installed into the local python instance.
! pip install --user <package>
The ! tells the notebook to execute the cell as a shell command.

you've got at least 3 versions of Python installed (the system version, a copy of 3.7 and 3.8). you need to figure out which is which, i.e. what you've done to your system!
to know which version of Python is being run you can use something like (from your shell/command prompt, not in Python):
which python3
see here for an explanation and alternatives. this tells you where some version of Python is, but you can also ask for pip3 and conda, jupyter, etc, to see where those have ended up, and to make sure you're running the right one. note that this involves your "shell's path" which you can customise so it picks the right one
next you need tools to figure out the equivalent "within Python". Python libraries aren't independent programs/executables (i.e. what $PATH determines) so this is a seperate set of options
to display where Python looks for code you can do this (inside Python):
import sys
print(sys.path)
see here for more info about what's going on here
note that what happens when you "open jupyter notebook by clicking on the icon with Anaconda Navigator" is a bit more difficult to debug. sys.executable might be useful to figure out what's going on
if you know xkcd, we're sort of in this state

Related

Why doesn't import gym work; however, the command pip install gym was done and executed

I have went to the terminal and used,"pip install gym", and it successfully installed gym . When I go to use the, "import gym," command, I get an error when running the code at that line.
Exception has occurred: ModuleNotFoundError
No module named 'gym'
I have looked at other posts and they all say it should work after ruining,"pip install gym." Although mine successfully installed, it still doesn't work.
You need to verify that you are using the same version of python that you did pip install with. In VS Code, you can select the Python version in the upper right corner. Simply type "python --version" into the console to verify the version. You might want to use Anaconda because someone libraries like Tensorflow only fully-support Anaconda.
if you have pip installed it, there is no way that it will not work. Unless you've installed it to a virtual environment and running your program without it and vice versa. Or you've installed to a different version of python on your system.

Why can I not import a module (Batman) in Jupyter Notebook

I'm trying to import a module (batman, for model transits) in jupyter notebook, but its telling me file not found. However when I use the anaconda terminal it imports without issues
This is the error I get from Jupyter Notebook
I've tried reinstalling the package, and I've tried to find a kernel in which it works, but I'm not sure how to go about that
any help is appreciated
Try checking if the module installed properly by running this:
python -c 'import batman; batman.test()'
the batman module also seems to have some issues when installing on windows.
Read this github if you're on windows https://github.com/lkreidberg/batman/issues/26
I've faced the same issue when I use Jupyter in an Anaconda environment. Jupyter Notebook doesn't use the same kernel as Anaconda and I had to specify it manually.
What you can do is comparing the Python version on your Anaconda environment by running this command in your shell:
python --version
And in your Notebook :
import sys
print(sys.version)
Are the versions different ? If so, you must add your Anaconda kernel to your Notebook :
ipython kernel install --name "local-anaconda-env" --user
Then select it in your Notebook.
I was facing the same issue in VSC. What I did is that first I moved the folder(In your case batman) to where VSC had been installed on my laptop. Then I changed the default directory to where VSC was installed on my laptop by running the following code in VSC.
import os
os.chdir("The path where VSC is installed")
Also If you could try to do the code in a python file instead of jupyter notebook
In a cell within your notebook run the following:
%pip install batman-package
Based on here.
And the use of modern pip install magic to handle installing to correct enviornment from inside a notebook, see here.
For others, hitting here and not seeing what they want/expect.
Above is for batman:
Bad-Ass Transit Model cAlculatioN. Associated Github Repo is here, and Python Package Index page.
This is a poor name for a package as at least three developers have claimed this. Also see:
Anaconda install for Batman at conda-forgeBayesian Analysis Tool for Modelling and uncertAinty quaNtification
BATMAN: A Python deployment toolbelt -Batman 0.7.0 at the Python Package Index
Maybe those links will help you.

ModuleNotFoundError despite package installation

For an unknown reason, some functions of the Python package GeoPandas were not working on my PC any more. I therefore deinstalled and reinstalled Anaconda and also installed the Python package GeoPandas via conda create -n geo-env -c conda-forge geopandas which is now check-marked as installed in the Anaconda package overview.
However, when I run import geopandas in Spyder I receive the following error:
runfile('untitled0.py', wdir='/Downloads')
Traceback (most recent call last):
File "untitled0.py", line 1, in <module>
import geopandas
ModuleNotFoundError: No module named 'geopandas'
Does anyone have an idea what I can do? I made sure I am running the correct Anaconda environment.
Not familiar with Spyder and haven't used Anaconda much, but I got curious. The first thing is that you want to install the Spyder-kernel as described here in your geo-env environment (you might already have done that). Great, all done, you load up Spyder, change your runtime to the correct python in your environment and... It doesn't work.
From my testing, Spyder is not very dynamic, so I had to restart Spyder once I had set it up to the correct environment. On restart, you will notice that the iPython window attempts to load the kernel and then it will work.

Installing and using npTDMS with Spyder - anaconda3

I have successfully installed npTDMS as seen here:
https://pypi.org/project/npTDMS/
If I try to install with command line using either pip or conda I get a message confirming it is already installed, and I see the package at D:\Users\username\anaconda3\pkgs.
On Spyder I have tried using the PYTHON path manager (in the tools menu) to add both:
D:\Users\username\anaconda3\pkgs\nptdms-1.2.0-pyhd8ed1ab_0
D:\Users\username\anaconda3\pkgs\nptdms-1.2.0-pyhd8ed1ab_0\site-packages\nptdms
As far as I understand this should be enough to use the npTDMS package, however when I try to import I get a ModuleNotFoundError: No module named 'nptdms'.
I'm fairly new to python overall so maybe there is a simple thing I am missing.
This issue comes when there are multiple versions of python or any package you are running at current time. The same issue was with me too when the IDLE was not able to detect the position of where the package is installed.
My suggestion is that uninstall all the versions of python and the module you are using and install them again. Otherwise shift to any other IDLE, in my recommendation - Visual Studio Code.

Actually opening spyder IDE

I've been trying to follow instructions on here and here to install spyder.
I downloaded the Source Code zip file from the first link because that's what I believe is the dmg installer. I don't know what to do with any of the files in the zip file once I download them though.
I already have pip3 installed so I used the command
pip3 install -U spyder
When I run the commmand
/User/Downloads/spyder-3.2.8/scripts/spyder
I get the error
Traceback (most recent call last):
File "/User/Downloads/spyder-3.2.8/scripts/spyder", line 2, in <module>
from spyder.app import start
ImportError: No module named spyder.app
To my understanding, spyder is supposed to be a python IDE that I am expecting to look something like this
I'm just trying to get the IDE running so that I can write my program within it to help me program easier. The only IDE i've used before is eclipse so maybe I'm not thinking of an IDE the right way, but I'm expecting something kind of like eclipse.
(Spyder maintainer here) I think our installation instructions are quite clear, but I repeat them here for you again:
If you are new to Python or the Scientific Python ecosystem, we strongly recommend you to install and use Anaconda. It comes with Spyder and all its dependencies, along with the most important Python scientific libraries (i.e. Numpy, Pandas, Matplotlib, IPython, etc) in a single, easy to use environment.
After installing Anaconda, you need to open the Anaconda Navigator (using Spotlight) and run Spyder from there. Another option is to open Terminal.app and run spyder or spyder3 there too.
You don't need to have our source code at all to use Spyder because it is installed by Anaconda or pip.
Finally, I don't know what Python version you used to run pip3 install -U spyder, but you should really remove what you installed with it and use Anaconda instead. As we said in our installation instructions too:
We also support pip, but please be aware that pip installations are for advanced users with good knowledge of all Spyder dependencies.
This clearly seems to be not your case.

Categories

Resources