Jupyter + rpy2 outputs to command prompt instead of notebook cell - python

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.

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

Unable to Import Module in Jupyter Notebook: Two Different Directories

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.

Running pyspark in (Anaconda - Spyder) in windows OS

Dears,
I am using windows 10 and I am familiar with testing my python code in Spyder.
however, when I am trying to write ïmport pyspark" command, Spyder showing "No module named 'pyspark'"
Pyspark is installed in my PC and also I can do import pyspark in command prompt without any error.
I found many blogs explaining how to do this in Ubuntu but I did not find how to solve it in windows.
Well for using packages in Spyder, you have to install them through Anaconda. You can open
"anaconda prompt" and the write down the blew code:
conda install pyshark
That will give you the package available in SPYDER.
Hi I have installed Pyspark in windows 10 few weeks back. Let me tell you how I did it.
I followed "https://changhsinlee.com/install-pyspark-windows-jupyter/".
So after following each step precisely you can able to run pyspark using either command promp or saving a python file and running.
When you run via notebook(download Anaconda). start anacoda shell and type pyspark. now you don't need to do "ïmport pyspark".
run your program without this and it will be alright. you can also do spark-submit but for that I figured out that you need to remove the PYSPARK_DRIVER_PATH and OPTS PATH in environment variable.

How to specify python3 kernel in jupyter in pyCharm?

Here is my setting
and this is my script
I am trying to use jupyter notebook in pyCharm, but it kept using python2 instead of python3.
Any idea about this problem?
Add:
this pic is running jupyter notebook in chrome.
My problem was that I had multiple kernels, and PyCharm launches the default kernel. One approach might be to configure PyCharm to specify the kernel of choice to start up, I didn't investigate how to do that. I simply changed the default kernel in Jupyter and this worked for me (I have a virtualenv for tensorflow). c.MultiKernelManager.default_kernel_name = 'tensorflow'.
The preferences image you show is indeed how you would setup your interpreter for PyCharm, but that's not what the output/logging of PyCharm looks like. I'm guessing that's a jupyter-notebook display, which means you are running into the issue in jupyter-notebook and not PyCharm. So you need to change your setup for jupyter. Based on some quick searching pip install jupyter will install a python 2.7 version of jupyter. Sounds like what you want is
pip3 install jupyter
which will install the python3 version for you. You will likely have to uninstall your current version of jupyter.
When you kick off Jupyter-notebook from within PyCharm there is a configuration which is created. If the configuration is initially 2.7 ( I think it defaults to the current interpreter), and then keep using that same configuration, it wouldn't matter the state of the current project interpreter because it would be using the value saved in the run configuration.
You can modify your run configuration by
Run | Run...
Edit Configurations...
Select your Jupyter Notebook run configuration on the left (here is untitled4)
Make sure the python interpreter is correct here on the right
I was able to start a jupyter notebook like this and get it to output python 3 by doing this. Hope this is what you are needing.

Jupyter Notebook does not work fully in Pycharm

I am following these steps to run IPython in the Pycharm IDE. On pressing run button in any cell, I do get below pop-up window
According to the mentioned guide (given link), On pressing OK, I should get following message
But, I am not getting any type of message/response. So, I am not able to see the output of any cell contents. Does anyone know why I am not getting any response on selecting OK?
System Information:
OS: MAC OS 10.10.3
Python: 2.7.10 using Anaconda 2.3.0
Ipython: 4.0.0
Pycharm: pycharm community addition 2016.2
I solved following:
First I followed the reply to the issue jupyter-notebook No such file or directory: 'conda', in short you should do:
conda update nb_conda nb_conda_kernels nb_anacondacloud
Then, I ran my jupyter notebook in my browser. I took note where the Jupyter notebook was running, i.e.
The Jupyter Notebook is running at: http://localhost:8888/
Then I started a new Jupyter Notebook in PyCharm2016.2.2, and when it asked about the Jupyter Notebook URL I changed the default http://127.0.0.1:8888 to http://localhost:8888/
and finally it worked.
For the PyCharm Jupyter notebooks, to see results you need to open up a jupyter notebook.
The reason why you don't just do all your code in Jupyter Notebooks is because Pycharm helps with LaTex integration and many more functions.
According to this answer:
Jupyter notebook server not working with latest PyCharm 2017.2 update. How do I solve this?
You need a package named ”notebook“. I had the same issue and I solved it by installing the package.

Categories

Resources