%run command to run .ipynb file forces .py extension - jupyter-notebook

I am trying to execute a simple run command:
%run /WaterfallViz.ipynb
But no matter how I format it I keep getting the same error:
ERROR:root:File `'/WaterfallViz.ipynb.py'` not found.
How do I prevent the .py extension to insert itself at the end of the file name?

If you have moved the notebook file where you %run another notebook, unfortunately Jupyter will still reference to the original directory path.
To fix this behavior, simply:
save your notebook
create a new notebook
copy the content from the original notebook
open the new notebook file
The %run now should work as the "directory path" is the one you are expecting, and the .ipynb file will be picked up instead of trying to open a .py file too.

I was having the same issue and I found out other ways to import the notebook that avoid this error, without needing to follow all the process recommended before (creating a new notebook, etc).
You need to first install a library:
pip install import-ipynb
Then, import it in your notebook:
import import_ipynb
And finally you can import your file:
import WaterfallViz

Related

Set Jupyter Lab working directory to the directory where Jupyter was started, not the parent directory of the notebook? [duplicate]

Question: How can one set the working directory of all notebooks opened in Jupyter Lab with a double-click, to be the project's folder, /myproject/, regardless of the notebook's subfolder within that parent folder? The working directory is identified by !pwd on Linux/Mac or !cd on Windows.
Context:
The Jupyter Lab sessions is initiated from the project's folder, by: /myproject/jupyter lab.
I am not looking for changing the working directory within the notebook with code (e.g. with !cd.. or using os), but change the settings of Jupyter Lab, such that the all kernels will start with the folder from where Jupyter Lab was initiated.
This is useful for being able to use consistent relative folders, regardless of the subfolder they are referred from. For example, for loading data in a specific subfolder.
I agree that this is often a preferred approach! I always have my notebooks configured to work like that because it:
makes it easy to specify paths to data and for outputs and
allows moving a notebook between directories without the need to change the paths
makes jupyterlab-lsp code intelligence work more reliably
I have a python module called make_paths_absolute.py with the following contents:
from os import chdir
from pathlib import Path
def make_paths_relative_to_root():
"""Always use the same, absolute (relative to root) paths
which makes moving the notebooks around easier.
"""
top_level = Path(__file__).parent
chdir(top_level)
make_paths_relative_to_root()
And in the first cell of every notebook, I add a line import make_paths_absolute. I like it this way because it makes it:
reproducible: anyone who copies/clones my project will be able to run the code without the need to customize anything in their Jupyter environment
work with all notebook interfaces (whether JupyterLab/RetroLab/classic Notebook)
is quite obvious for anyone reading the notebook that they should expect the paths to be absolute (=relative to the root).
To make that work you first need to set PYTHONPATH pointing to the root of the repository when starting JupyterLab. To do so on Linux/Mac you can prepend the jupyter lab command with:
PYTHONPATH=/path/to/your/lab/root:$PYTHONPATH jupyter lab
Alternatively, you can specify PYTHONPATH in kernel.json (either by modifying the default kernel specification or creating a copy of it first, see https://stackoverflow.com/a/53595397).
PS. The above is a simplification of my setup. In reality, I store make_paths_absolute.py in helpers package (so there is also an empty helpers/__init__.py and there is extra .parent) together with other initialization-time code, including a notebook called notebook_setup.ipynb which contains things like %matplotlib inline, pandas customizations (like making sure it uses stable sort), warning filters etc. So my every notebook starts with:
import helpers.make_paths_absolute
%run helpers/notebook_setup.ipynb
and I am really liking this setup working like that for two years now without any problems.
There is a feature request for making this easier at: https://github.com/jupyterlab/jupyterlab/issues/11619.
The most easiest solution in my opinion
Open Notepad
Paste the command "jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10"
Save the notepad file with an extension of ".bat" instead of ".txt"
Paste the file in which directory you want to initialize your jupyter
Double click and open the ".bat" file
Jupyter opens with desired directory as base
This way you control the jupyter root directory as and when required and don't
really have to perform any manual settings
Hope this helps
".bat" file created on Desktop
enter image description here
".bat" file double clicked and executed
enter image description here
jupyter opens with Desktop as the intended base directory
enter image description here

pyspark: how to show current directory?

Hi I'm using pyspark interactively. I think I'm failing loading a LOCAL file correctly.
how do I check current directory, so that I can go to browser to take a look at that actual file?
Or is the default directory where pyspark is? Thanks
You can't load local file unless you have same file in all workers under same path. For example if you want to read data.csv file in spark, copy this file to all workers under same path(say /tmp/data.csv). Now you can use sc.textFile("file:///tmp/data.csv") to create RDD.
Current working directory is the folder from where you have started pyspark. You can start pyspark using ipython and run pwd command to check working directory.
[Set PYSPARK_DRIVER_PYTHON=/path/to/ipython in spark-env.sh to use ipython]
import os
cwd = os.getcwd()
print(cwd)

RQDA batch file import not working

I am trying to import multiple files into RQDA like this:
#install.packages("RQDA")
library(RQDA)
#import list of files into program
files<-list.files("C:\\Users\\blah\\Desktop\\SNA_R_Class_code\\Texts")
RQDA()
setwd("C:\\Users\\blah\\Desktop\\SNA_R_Class_code")
openProject("me_classify_class_texts.rqda") #this is a completely empty project I just created
write.FileList(files)
When I look at the RDQA GUI it shows no project open, I get the following message in RStudio console: NA exists in the database!
When I manually start RQDA and open a project the run the final line of the code above I get the same result.
I can import files through the RQDA GUI fine however. What am I doing wrong with the command?
I needed to set a different file title for every file before it would import correctly

How can I import a directory (with subdirectories) into ipython notebook

I was wondering if there is a better way to import a folder (which has subdirectories) that contains notebook files. Currently I have to create a folder on the notebook dashboard and then import a notebook file and repeat for all other files and folders. There are too many to do this manually. Is there a better way to import an entire folder in a couple of command lines or via GUI?
Version of ipython notebook 3.0.0 on python 2.7

Command Line to Open .py with IPython

I would like to use the great IPython Web Interface to open, evaluate, edit and save the following "myfile.py" (see below) avoiding the annoying process: Create an .ipynb > import myfile.py to it > make some evaluation or edition > export to .py > remove unnecessary code lines and finaly get again the following content (myfile.py):
import os
# <codecell>
# Number division
print(4/5)
# Number Plus
print(1+40)
Is there a command line to do so?
Notes:
I want work ONLY with .py files, any solution with store/work with .ipynb (JSON files) not be welcome.
Suggestions for other programs will be very welcome.
On stable version, use the --script flag, it always save .py file wihthout having to go through the export process. Still it also save the .ipynb file along side.
On dev version there are now pre-save hook that allows you to do whatever you want for saving .
To automatically load .py files, you will have to write your own Notebook File Loader backend that accept .py files.

Resources