What is the difference between !{} and !$ in jupyter notebook? - jupyter-notebook

I am running this command in my jupyter notebook in two ways:
! {sys.executable} -m pip install numpy
and
! $sys.executable -m pip install numpy
both gives same output.
What is the difference between the two?

Related

How to enable Stata kernel in jupyter notebook

I am using MacOS and try to use Stata in the jupyter notebook. I went over the following step to do so.
First, in the terminal:
pip install stata_kernel
python -m stata_kernel.install
conda install -c conda-forge nodejs -y
jupyter labextension install jupyterlab-stata-highlight
pip install ipystata
And then in my jupyter notebook:
import ipystata
from ipystata.config import config_stata
config_stata('/Macintosh HD⁩/Applications/Stata/StataSE')
import ipystata
%%stata
display "Hello, I am printed in Stata."
But I keep getting the error message
UsageError: Line magic function %%stata not found.
I am not sure if this is path problem or something else.
Could you help me to fix the problem?

adding conda environment via cmd line to jupyter doesn't work

Trying to get OSMnx into jupyter conda environment.
But conda environment not set correctly.
On command line:
Created conda environment using OSMnx installation steps
I cloned the environment to rename it 'realestate'
Added realestate to Jupyter:
> conda activate realestate
> conda install -c anaconda ipykernel
> python -m ipykernel install --user --name=realestate
> which python
/opt/anaconda3/envs/realestate/bin/python
On Jupyter the realestate environment shows up... but when I run
!which python
I get
/opt/anaconda3/bin/python
I've created some conda environments inside of Jupyter(Conda) and they show up as
Python [conda env: otherenv]
that then produces right path
!which python
/opt/anaconda3/envs/otherenv/bin/python
What am I missing?
PS. OSMnx install is not a simple conda install
conda config --prepend channels conda-forge
conda create -n ox --strict-channel-priority osmnx
From what I can tell, it sounds like you're trying to have OSMnx, Jupyter, and all the packages in the anaconda metapackage installed together in a single conda environment. If so, just create the conda environment with all those packages in one line, then install the ipython kernelspec in it:
conda config --prepend channels conda-forge
conda create -n realestate --strict-channel-priority osmnx anaconda jupyter
conda activate realestate
python -m ipykernel install --sys-prefix --name realestate --display-name "Python (realestate)"
jupyter kernelspec list

Jupyter kernel uses different Python version than Conda environment

I am not able to link Jupyter kernels to their parent Conda environments. After creating a new kernel linked to Conda environment, I'm getting a different version of Python and its dependencies inside Jupyter lab.
Here are the steps I followed:
Created a conda environment using:
conda create -n nlp python=3.6
conda activate nlp
(nlp) ➜ ~ python --version
Python 3.6.9 :: Anaconda, Inc.
(nlp) ➜ ~ which python
/anaconda3/envs/nlp/bin/python
Inside the environment I created a Jupyter kernel with:
(nlp) ➜ ~ python -m ipykernel install --user --name=nlp
Installed kernelspec nlp in /Users//Library/Jupyter/kernels/nlp
Investigating the created json file for the kernel:
(nlp) ➜ ~ cat /Users/<username>/Library/Jupyter/kernels/nlp/kernel.json
{
"argv": [
"/anaconda3/envs/nlp/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "nlp",
"language": "python"
}%
It seems to be pointing to the environment version of Python
But when I start Jupyter Lab and select the nlp kernel, I get a different version of Python and some dependencies are missing
!python --version
Python 3.5.6 :: Anaconda, Inc.
!which python
/anaconda3/bin/python
Could you please try the following steps:
conda activate nlp
conda install ipykernel
ipython kernel install --name nlp --user
After these steps please try changing the kernel again in jupyter lab to "nlp".
Thanks.
this behavior is actually normal in Jupyter lab.
If you run
import sys
print(sys.version)
!python --version
in a notebook, the print statement will give you the Python version of the conda env, while the second will give you the Python version of your base env.
The easiest workaround for this is to simply pip install jupyterlab in your conda env and then run jupyter lab in your conda env. Then, there will not be a mismatch in Python versions between the new "base" env and the conda env which will help clear up any DLL problems.
It's probably not best practice, but you do what you gotta do when working with legacy code, ig.
Can you try this :
# in base env
conda install nb_conda_kernels
conda activate nlp
conda install ipykernel
conda install ipywidgets
# install kernelspec
python -m ipykernel install --user --name nlp --display-name "nlp env"
When you run jupyter notebook, you will see 2 nlp kernels. Use the one with "Python [conda:env:nlp]"

How to install R-magic in jupyter-notebook?

I created a new environment in jupyter called "rpy2" and try to use %%R magic but the installation is failed.
QN: How to use %%R magic in jupyter-notebook ?
Platform: MacOS High Sierra
Steps
jupyter kernelspec list
conda create -n rpy2 python=3.7
source activate rpy2
which pip
/Users/poudel/miniconda3/envs/rpy2/bin/pip install rpy2
conda install ipykernel
python -m ipykernel install --user --name rpy2 --display-name "Py37rpy2"
jupyter-notebook
Issues
# Now
import rpy2 shows no error
import rpy2.rinterface # gives error
Note
# I have checked all the files given in error log, all of them exist
492 ls /Users/poudel/miniconda3/lib/R/lib/libR.dylib
493 ls /usr/lib/libiconv.2.dylib
494 ls /Users/poudel/miniconda3/lib/R/lib/libR.dylib
495 /Users/poudel/miniconda3/lib/R/lib/libR.dylib
496 ls /Users/poudel/miniconda3/lib/R/lib/libR.dylib
All shows given file, and none is missing.
Error
OSError: cannot load library '/Users/poudel/miniconda3/lib/R/lib/libR.dylib': dlopen(/Users/poudel/miniconda3/lib/R/lib/libR.dylib, 2): Symbol not found: _libiconv
Referenced from: /Users/poudel/miniconda3/lib/R/lib/libR.dylib
Expected in: /usr/lib/libiconv.2.dylib
in /Users/poudel/miniconda3/lib/R/lib/libR.dylib
Update
As suggested by #akrun, I tried using pyper but it takes infinite time to load a libary and when stopped shows that it has broken pipe.
pip install pyper.
if you're still looking for a solution, here's what you need.
Start the notebook with
%load_ext rpy2.ipython
Then run R cells using the R magic function
%%R
library("tidyverse")
This works for the latest rpy2 version (3.4.5).

jupyter not found after pip install jupyter

After many different ways of trying to install jupyter, it does not seem to install correctly.
May be MacOS related based on how many MacOS system python issues I've been having recently
pip install jupyter --user
Seems to install correctly
But then jupyter is not found
where jupyter
jupyter not found
Not found
Trying another install method found on SO
pip install --upgrade notebook
Seems to install correctly
jupyter is still not found
where pip /usr/local/bin/pip
What can I do to get the command line jupyter notebook command working as in the first step here: https://jupyter.readthedocs.io/en/latest/running.html#running
Short answer: Use python -m notebook
After updating to OS Catalina, I installed a brewed python: brew install python.
It symlinks the Python3, but not the python command, so I added to my $PATH variable the following:
/usr/local/opt/python/libexec/bin
to make the brew python the default python command (don't use system python, and now python2.7 is deprecated). python -m pip install jupyter works, and I can find the jupyter files in ~/Library/Python/3.7/bin/, but the tutorial command of jupyter notebook doesn't work. Instead I just run python -m notebook.
My MacOS has python 2.7, I installed python3 with brew, then the following commands work for me
brew install python3
brew link --overwrite python
pip3 install ipython
python3 -m pip install jupyter
You need to add the local python install directory to your path. Apparently this is not done by default on MacOS.
Try:
export PATH="$HOME/Library/Python/<version number>/bin:$PATH"
and/or add it to your ~/.bashrc.
Try solving this with Conda or Poetry.
Poetry makes it a lot easier to manage Python dependencies (including Jupyter) and build a virtual environment.
Here are the steps to adding Jupyter to a project:
Run poetry add pandas jupyter ipykernel to add the dependency
Run poetry shell to create a shell within the virtual environment
Run jupyter notebook to to fire up a notebook with access to all the virtual environment dependencies
The other suggested solutions are just band-aids. Conda / Poetry can give you a sustainable solution that's easy to maintain and will shield you from constant Python dependency hell.

Resources