Configuring Jupyter default imports - python

How do I tell Jupyter (console and notebook) to import some Python packages by default? I would like to do this using only the .jupyter folder

I personally couldn't find a way to do so only using the .jupyter folder. You have to specify the commands to be executed on startup in the .ipython folder anyway:
Create ~/.ipython/profile_default/ipython_config.py if it not exists
Add something like this:
c = get_config()
c.InteractiveShellApp.exec_lines = [
'import numpy as np\n'
'import scipy as sp\n'
'import matplotlib as plt\n'
]
You can also specify any valid commands here, not only imports.

A bit of background:
Jupyter provides the UI/environments such as the console and notebook. It defers to what it calls kernels for execution.
IPython provides the default (Python) kernel for Jupyter.
Jupyter configuration doesn't affect kernels directly, but each kernel may have its own configuration.
The IPython configuration resides in your .ipython directory. The quickest way to add code to run on startup of IPython (affects IPython sessions in the terminal and notebook) is to add startup files to your IPython profile.
Create the default profile, if it doesn't exist already (it probably does):
ipython profile create
Make a Python script ~/.ipython/profile_default/startup/whateveryouwant.py and add any imports or other commands in there that you would like to have ready whenever you start IPython. IPython will run this script and any others in that directory every time it starts up.

Related

Jupyter notebook set project dependency

I have two different project folders. I want to make one of the projects a root so that when I import something from first project in the second project it does not show ModuleNotFoundError. For example, In PyCharm we can set project dependencies. Is there way to do so in Jupyter Notebook?
The best way to handle this in notebooks or jupyterlab would be to set the PYTHONPATH environment variable. For example, on MAC or unix-based OS, using bash, do this:
export PYTHONPATH="${PYTHONPATH}:/path/to/project"
Then re-load the notebook. Adding your project path to the PYTHONPATH variable will allow you to import any modules that are specified in this variable.
Additionally, you can set this variable in a jupyter notebook itself by entering the bash command into a notebook cell, prepended with the ! symbol like this:
! export PYTHONPATH="${PYTHONPATH}:/path/to/project"
Then executing the cell.

How to place custom Jupyter kernels inside virtual environment?

I have a custom Jupyter kernel which runs IPython using a custom IPython profile which uses a matplotlib stylesheet.
I know to run this successfully normally I would put:
The matplotlib stylesheet in ~/.config/matplotlib/stylelib/
The IPython profile in ~/.ipython/
The kernel json in ~/.jupyter/kernels/my_kernel/
But I am doing this as part of larger program which runs in a virtualenv, and if I put the things as above then any notebook server running on the computer will be able to see the custom kernels, even if it is running outside the venv. I don't what this because I don't want my program to interfere with other notebooks on the computer.
I think what I need to do is put the things above somewhere equivalent inside the venv but I can't figure out where they should go. Doe anyone know where they would go? Or is this just a thing IPython/Jupiter can't/won't do?
It's probably worth mentioning that in the case of the stylesheet for example I don't want to just put it in the working directory of my program (which is one option matplotlib offers).
You can put kernelspecs in VIRTUAL_ENV/share/jupyter/kernels/ and they will be made available if the notebook server is running in that env. In general, <sys.prefix>/share/jupyter/kernels is included in the path to look for kernelspecs.
You can see the various locations Jupyter will look, you can see the output of jupyter --paths:
$ jupyter --paths
config:
/Users/you/.jupyter
/Users/you/env/etc/jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/Users/you/Library/Jupyter
/Users/you/env/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/Users/you/Library/Jupyter/runtime
Kernelspecs are considered data files, and will be found in any of those directories listed under data:, in a kernels subdirectory, e.g. /usr/local/share/jupyter/kernels.

How do I get IPython profile behavior from Jupyter 4.x?

There was official(?) recommendation of running an IPython Notebook server, and creating a profile via
$ ipython profile create nbserver
as recommended in http://ipython.org/ipython-doc/1/interactive/public_server.html. This allowed for very different and very useful behavior when starting an IPython Notebook via ipython notebook and ipython notebook --profile=nbserver.
With Jupyter 4.0, there's a change and there are no longer profiles. I've found the conversation https://gitter.im/ipython/ipython/archives/2015/05/29 which has user minrk saying:
The .ipython directory has several things in it:
multiple config directories (called profiles)
one 'data' directory, containing things like kernelspecs, nbextensions
runtime info scattered throughout, but mostly in profiles
Jupyter follows more platform-appropriate conventions:
one config dir at JUPYTER_CONFIG_DIR, default: .jupyter
one data dir at JUPYTER_DATA_DIR, default: platform-specific
one runtime dir at JUPYTER_RUNTIME_DIR, default: platform-specific
And a rather cryptic remark:
If you want to use different config, specify a different config directory with JUPYTER_CONFIG_DIR=whatever
What's the best way to get different behavior (say, between when running as a server vs normal usage)?
Will it involve running something like:
$ export JUPYTER_CONFIG_DIR=~/.jupyter-nbserver
$ jupyter notebook
whenever a server 'profile' needs to be run? and
$ export JUPYTER_CONFIG_DIR=~/.jupyter
$ jupyter notebook
whenever a 'normal' profile needs to run? Because that seems terrible. What's the best way to do this in Jupyter 4.0?
Using some code from this blog post http://www.svds.com/jupyter-notebook-best-practices-for-data-science/ and updating it. The easiest solution appears to be to create an alias, like:
alias jupyter-nbserver='JUPYTER_CONFIG_DIR=~/.jupyter-nbserver jupyter notebook'
So now you can run the jupyter notebook with a different config via the simple command jupyter-nbserver.
A more robust solution might involve creating a bash function that changes the environment variable, checks whether there's a config file, if not creating one, then executing, but that's probably overkill. The answer that I give on this related question https://stackoverflow.com/a/32516200/246856 goes into creating the initial config files for a new 'profile'.

start ipython notebook with python file

I am not very familiar with python/ipython but somebody was asking me whether it is possible to start an ipython notebook with a specific python file. It then could be used for debugging.
another software then would create a .py-file in the temp folder and would call an ipython notebook with this file.
Is it possible or does it make sense at all?
Since the question is quite broad and asking for recommendations, here are my suggestions:
cross-platform nbopen, which opens ipynb using command-line or optional explorer integration:
https://github.com/takluyver/nbopen
Please note that I have one open ticket for complete Windows explorer integration:
https://github.com/takluyver/nbopen/issues/12
[copied from github page]
Installation:
pip install nbopen
Usage:
nbopen AwesomeNotebook.ipynb
run ipynb without launching the browser interface, with many useful options:
https://github.com/paulgb/runipy
[copied from github page]
Installation:
$ pip install runipy
To run a .ipynb file as a script, run:
$ runipy MyNotebook.ipynb
To save the output of each cell back to the notebook file, run:
$ runipy -o MyNotebook.ipynb
To save the notebook output as a new notebook, run:
$ runipy MyNotebook.ipynb OutputNotebook.ipynb
To run a .ipynb file and generate an HTML report, run:
$ runipy MyNotebook.ipynb --html report.html
If you're talking about launching an iPython notebook server via Python, I use this:
#!/usr/bin/env python
from IPython.terminal.ipapp import launch_new_instance
from IPython.lib import passwd
from socket import gethostname
import warnings
warnings.filterwarnings("ignore", module = "zmq.*")
sys.argv.append("notebook")
sys.argv.append("--IPKernelApp.pylab='inline'")
sys.argv.append("--NotebookApp.ip=" + gethostname())
sys.argv.append("--NotebookApp.open_browser=False")
sys.argv.append("--NotebookApp.password=" + passwd())
launch_new_instance()
Obviously you can change the arguments if you so desire.
At my work we have one use case that does what you're saying--automatically generates a python file, then loads a new ipython server for the user to access it. However, it's a pretty special use case--for normal debugging, I would recommend just starting in iPython and not making your *.py file until the bugs are gone.
OR
If you're talking about actually automatically navigating to the page that corresponds to a python file made available by an ipython notebook server, then (1) make sure you're using ipython 2, and (2) figure out what you're desired url is (it should be deterministic) and (3) use the webbrowser module to navigate to that url.
import subprocess, os
def executeJupyter():
env_dir = "../main_env_dir/"
os.chdir(env_dir)
# source jupyter_env/bin/activate
env_activate = "jupyter_env/bin/activate_this.py"
activate_env = exec(open(env_activate).read(), {'__file__': env_activate})
# Open jupyter notebook as a subprocess
openJupyter = "jupyter notebook"
subprocess.Popen(openJupyter, shell=True)
executeJupyter()
Make sure you change the env_dir (directory where you have the env of your jupyter notebook) and the env_activate to your own.
To start a ipython notebook with a certain notebook directory, use the --notebook-dir command line option:
ipython notebook --notebook-dir=/Users/harold/temp/

ipython console2 no module named Ipython

I've been using ipython notebook with console2 for a while now and recently installed a different version of python and now my console is giving me an error saying "No module named IPython". I think the path has been changed or something, but I don't know how to fix it. Any help is greatly appreciated!
I am pulling the answer out of the comments.
Point your PATH system variable to the correct version of Python. This is accomplished (on Windows) by going to System Properties -> Advanced -> Environment Variables. If you already have a Python directory in there, modify it to the correct, new path. Otherwise, append it to the end of the existing string. Do not remove what is already present.
If you are using anaconda for python the ipython installation is on "Scripts" folder
To get the console2 running with ipython you all have to do to start cmd in silent mode by configuring a new tab in the console2 settings window on win7
here is my config for the shell:
cmd.exe /k C:\Anaconda\python.exe "C:\Anaconda\Scripts/ipython-script.py"
for the startup dir i have just defined the workspace i am using:
D:\python-workspace

Categories

Resources