Unable to Import Module in Jupyter Notebook: Two Different Directories - python

New to python, so bear with me.
I have installed Anaconda and Jupyter Notebook successfully, and I have also set up what I think is the correct Path (at least for Anaconda). From an Anaconda prompt, python works as expected, and I can import the modules I need. However, I get a ModuleNotFoundError when I try to import certain modules in Jupyter Notebook (such as cx_Oracle). I think the issue is they are running off of two different directories.
When I run sys.executable in the Anaconda prompt, I get this directory: C:\Users\username\Documents\Anaconda\python.exe
When I run sys.executable in Jupyter Notebook, I get this directory: C:\Users\username\AppData\Local\Continuum\anaconda3\python.exe
Shouldn't these be the same? I would like Jupyter Notebook to execute on the same directory the Anaconda prompt does: C:\Users\username\Documents\Anaconda\python.exe
So my question is: how do I change the Jupyter Notebook directory so it points to the same one Anaconda is using?
Thanks all. This issue has been driving me crazy.

Related

ModuleNotFoundError in VS code with Jupyter note book but not .py files

I am using visual studio code on windows, and I am trying to run a python code on jupyter-notebook. I have all packages installed and they work fine when it is a normal python file. But I need it to run as a notebook. Once I run the block, I immediately get the following error:
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_5668\1439934476.py in <module>
1 import csv
----> 2 from pandas import read_csv
3 from matplotlib import pyplot as plt
4 import numpy as np
5 import os
ModuleNotFoundError: No module named 'pandas'
I made sure that requirements are already satsified for this module, and I already tried running this line in python file with no issues. Therefore, how can I solve this issue? How can I let Jupyter notebook compiler to see where these packages actually are?
The installation of pip is related to the python path specified in the environment variable. You need to select the correct interpreter in vscode.
Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use.
From within VS Code, select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too):
Read docs for more details.
First you need to install pandas by running the command in your console:
pip install pandas
If you don't have pip installed on your computer then install it, otherwise you won't be able to download pandas or other modules
PS: check that when you downloaded python you chose the add to PATH option
Hoping I could help
If you have a problem running jupyter notebook file make sure
You have installed jupyter notebook from anaconda from this site
Once you installed that, open anaconda navigator and click environment tab
Install python and pandas
Jupyter Notebook works like environment different from VSCode
You can also run Jupyter Notebook file on VSCode by installing Jupyter extension in VSCode
My guess is you don't installed it yet in anaconda navigator, that's what makes it error

No module named pandas error even though I have it installed and interpreter set (Dataspell, Jupyter notebook/lab))

Package manager: conda
Using virtual environment: base conda environment
Errors found in the following IDEs: Dataspell, Jupyter notebook
- No errors in the following IDE: vs code
error:
import pandas as pd
no module named 'pandas'
First I'll go through the dataspell/jupyter notebook issue
Dataspell/jupyter notebook
Notice the executable. It's not using the conda python exe.
Notice the path. Reaffirms that it's not seeing the anaconda directory.
But when I open the interpreter settings for this workspace,
The kernelspec
Note: There's only one kernel spec directory
The file looks like this
It works in vscode
Again Notice the executable and path. Both are using/seeing the anaconda python exe.
So my question is this - Why isn't dataspell/jupyter notebook seeing anaconda as a path and using the base env? And how do I fix it?
The link in Wayne's comment worked. Just needed to create a new kernel by going into the virtual environment. https://stackoverflow.com/a/71733561/8508004 is the post that has the answer.

Python import pyd module fails in Jupyter, works in IDLE

My company provided a .pyd file to work with a new API of our internal software. I followed their instructions using Python's IDLE (and python in command prompt) and it worked without any issues. When I tried the same steps using a Jupyter Notebook, I get an error
# modname.cp38-win_amd64.pyd located in installation folder of foobar
import sys
import os
sys.path.append(r"C:\Program Files (x86)\foo\bar\bin64")
import modname
This is the point when IDLE continues and Jupyter Notebook says
----> 1 import modname
ImportError: DLL load failed while importing modname: The specified module could not be found.
I installed Jupyter notebook by using "pip install jupyterlab" and run it from a command prompt in Windows. I checked using print(sys.executable) and both IDLE and Jupyter are using the same Python installation, and I also tried running python directly in the command prompt in case python.exe (Jupyter and CMD's) and pythonw.exe (IDLE's version) differed, but it worked under command prompt.
I used print(sys.path) to check for path differences but everything in IDLE's path is also in Jupyter's. All of the previous answers point to things I have tried or assume the user is using Anaconda. I can continue without using a Jupyter Notebook but had hoped to try a new workflow.
UPDATE: More Information
I loaded the pyd in Dependencies (updated open source Dependent Walker), and all of the dlls were either in the current PATH folders or C:\Windows\System32. I added the System32 folder to the path using sys.path.append with the same result.
I also used print(os.environ) in both IDLE and Jupyter to look for any differences. IDLE had a 'Home' key (C:\Users\MyUserName) that Juptyer didn't have so I added it with the same result. Jupyter had a few others but they didn't seem relevant (terminal color, lang, etc.)

How can I pass the PYTHONPATH in Jupyter Lab/Notebook?

Ok, this must be silly but I really can't find a good way to do it.
When I open a Python session with Jupyter Notebook or a Jupyter Lab, I cannot have access to all my scripts on other folders which are generally included in PYTHONPATH.
If I run the Jupyter Notebook from Pycharm, it actually works and I can see that I have the correct PYTHONPATH by doing
import os
os.getcwd()
but if I do the same with a session started from CLI, I get a KeyError and PYTHONPATH not found.
This must have been solved a thousand times, how come I can't find anything about it?

Jupyter + rpy2 outputs to command prompt instead of notebook cell

Issue description
Whenever loading rpy2 in a Jupyter notebook, R code executed with a %%Rcell magic gets printed in the command prompt instead of the notebook cell. Plots show up properly in the notebook though.
I also noticed that the %R inline magic works properly, with code printed in the cell as expected.
Installation steps
Python 2.7.11 :: Anaconda 2.5.0 (64-bit)
notebook 4.1.0 installed via pip
R 3.3.1 (x64) located in Program Files
rpy2-2.7.8-cp27-none-win_amd64.whl installed via pip
PATH:
R_USER = C:\Users\myusername
R_HOME = C:\Program Files\R\R-3.3.1
Added C:\Program Files\R\R-3.3.1\bin\i386
You can use the RWinOut Jupyter extension. The following installation instructions are taken from the GitHub page:
You can run the following curl command from a Jupyter notebook cell to download the file to your working directory. You can also download it manually and put it there yourself.
!curl -O "https://raw.githubusercontent.com/vitorcurtis/RWinOut/master/RWinOut.py"
Once it's in your working directory, you can replace %load_ext rpy2.ipython at the top of your script with %load_ext RWinOut. Then you should be able to see the output of cells containing the %%R magic as normal.
This isn't a complete solution, but it's a workaround that might achieve the same effect. I'm not sure if this breaks some functionality, but it seems to work fine for me.
Tested with:
Windows 10 v.1809 Build 17763.503
Python 3.7.2
R 3.6.0
rpy2 2.9.5
One other option is to use the Windows Subsystem for Linux and launch your Jupyter notebook from there. This might not be desirable if you have to reinstall a lot of R and Python packages into a different environment, but will make it so the output will print correctly without requiring this hacky workaround.

Categories

Resources