I run Jupyter Notebook on a mac and I am trying to edit a notebook file from a git repo I am contributing to. However I am having trouble accessing the file from the Notebook interface.
Is there a way to access notebooks which are not in the default path on mac (navigation to arbitrary file locations works fine on windows) without changing the default directory to my git repo and without copying the file to my default directory and back every time I edit it?
It looks like direct navigation outside the path is impossible (Based on this post) but it seems that it should be possible to start the notebook from the given directory using the command line.
Things I've tried:
Directly typing the relative and absolute path into the web interface with respect to the tree (i.e. http://localhost:8889/tree/../../[path-to-file]
Renaming the file with the path as a prefix
Starting jupyter from the directory containing the notebook
... with no success as of yet.
Any help on this would be greatly appreciated.
it seems that it should be possible to start the notebook from the given directory using the command line
You can...
jupyter notebook /some/non-default/path
However, you can only reach sub-directories of that path with Jupyter
Starting jupyter from the directory containing the notebook
That should also work
If you python code needs to read files that are in parent directories, relative paths still resolve fine within the notebook kernel.
Related
I want to change dir to the parent dir of my jupyter notebook.
I can not take the notebook path using os.path.basename(os.path.dirname(os.path.realpath(__file__))) as __file__ is not defined.
How can I get the dir of the ipynb file I am using in order to os.chdir() to it?
You can't
https://github.com/ipython/ipython/issues/10123
The reason is because you're always running in the kernel, and in theory multiple notebooks could connect to that kernel.
However - by default if you're starting a notebook, the current working directory is set to the path of the notebook. So the closest, is to call os.getcwd()
I just created the most boring published notebook example to demonstrate this, you can see that, this notebook reflects it's path, and this one that's in a subdirectory also reflects the proper path
I'm using WingIDE for development and Ipython for running my scripts. I'm facing some inconvenience on several points:
Whenever I update my code, I have to reload my module in order to update it in IPython. To solve that I followed Jomonsugi's answer in the following link: How to automatically reload modules in IPython?
and it works.
Each time I launch IPython, my path is set to my exe's location and I have to cd to my script's directory.
I tried to change directory automatically when launching IPython by setting the parameter c.InteractiveShell.ipython_dir = <mypath> in ipython_config.py but it doesn't work.
Is it possible to set the default path? Is there a particular parameter to set in ipython_config.py?
One way is to use your startup.py file. It should be located somewhere like:
C:/Users/yourname/.ipython/profile_default
If it's not there already, create it. Then use
import os
os.chdir('C:/Users/mypath')
Those two lines will then be run at IPython startup.
Source: IPython.org > Startup Files
I’m sure Brad Solomon’s answer is right for his version of IPython, but I’ve just downloaded IPython with pip install ipython and my startup files are in a directory nested one deeper than his. My IPython version is 7.18.1, and the start-up files are located in ~/.ipython/profile_default/startup/. There is a README there which states
This is the IPython startup directory
.py and .ipy files in this directory will be run *prior* to any code
or files specified via the exec_lines or exec_files configurables
whenever you load this profile.
Files will be run in lexicographical order, so you can control the
execution order of files with a prefix, e.g.::
00-first.py
50-middle.py
99-last.ipy
That’s pretty self-descriptive, but I would add to this (for anyone coming here from here) that %load_ext autoreload is an IPython command, so you might want to create a file startup.ipy with contents something like this:
%load_ext autoreload
%autoreload 2
print('IPython startup file - created 2020/9/28')
I'm looking for a way to put my startup script in Jupyter. In IPython, I put it under the $IPYTHON_DIR/profile_default/startup/.
In Jupyter, it seems that the config file should be $JUPYTER_CONFIG_DIR/jupyter_notebook_config.py. However, I would like to use my startup file, which import a slew of Python libraries at the launch of the kernel.
Where can I put such file in Jupyter?
You can get the default startup script folder via this in jupyter notebook:
get_ipython().profile_dir.startup_dir
On my Windows PC, the folder is: C:\Users\myusername\.ipython\profile_default\startup
And do read the README file under the folder.
Files will be run in lexicographical order, so you can control
the execution order of files with a prefix, e.g.::
00-first.py
50-middle.py
99-last.ipy
So you can put a file named 00-xx.py under the folder.
I'm trying to set a custom starting directory in Jupyter Notebook. I have edited jupyter_notebook_config.py.
Removed # from line "c.NotebookApp.notebook_dir =", added parameter:
c.NotebookApp.notebook_dir = u'c:\\my\\chosen\\directory'.
But still doesn't work, console coming up with error, and jupyter starting in the default home directory.
I'm using Windows server 2008. According to the manuals, it should work.
Does anyone have a suggestion about my problem?
The followings steps work perfectly for me on Windows:
First find which directory Jupyter is looking in for your config file:
jupyter --config-dir
If there is no jupyter_notebook_config.py file in that directory, generate one by typing:
jupyter notebook --generate-config
Then edit the jupyter_notebook_config.py file and add something like:
## The directory to use for notebooks and kernels.
c.NotebookApp.notebook_dir = 'c:\\users\\rsignell\\documents\\github'
Then start your jupyter notebook from any directory:
jupyter notebook
and it will start in the directory you specified.
For more info see: http://jupyter-notebook.readthedocs.io/en/latest/config.html
Microsoft Windows
Open dos command line by typing cmd on windows explorer address bar. This will open command prompt with current path set to current folder. Type jupyter notebook --notebook-dir=%CD% on commandline to start jupyter notebook (ipython notebook) with current directory as notebook's starting directory
I also had the problem, and editing jupyter configuration file didn't work either.
My workaround is to make a batch file that goes to a specified directory, then start jupyter notebook from that directory.
You can use notepad to write the batch file, just save it as an all files and specify the extension as .bat
An easy way is also available from DOS prompt using copy con. First, access command prompt (usually by typing "cmd" and enter). Then:
copy con startjupyter.bat
after that you can specify your directory and start notebook from there, for example if your directory is D:\python_codes :
d:
cd python_codes
jupyter notebook
After that, save the file using CTRL+Z and Enter.
You can run the batch file by calling the name (startjupyter), or click it. For the latter, maybe put it in your desktop for easy access.
I had also problems with the solutions given here. My solution was quick and dirty then, but it works with Windows. I made a batch-file:
cd C:\[starting Directory]
jupyter notebook
stop
You can start Jupyter with a defined directory when you use different batch-files. For example:
cd C:\datascience
or
cd C:\browsergame
I am trying to load a .py file into a single cell in a jupyter notebook. I tried using the command %load training.py and I received the error:
"ValueError: 'training.py' was not found in history, as a file, url, nor in the user namespace."
I have a folder that contains all of the .py files that I want to be able to load into jupyter in C:\Users\Jeffrey\CharmTagger. I then moved one of the .py files out of the folder and into C:\Users\Jeffrey and the %load command found that file. Is there anyway I can get jupyter to look in folders? I don't want to clutter my User folder. Is there anywhere else I can put these files so that they can still be found by %load?
You can always use the full path:
%load C:\Users\Jeffrey\CharmTagger\training.py
When you type:
%load training.py
IPython looks into the current working directory. You can find it with:
%pwd
The magic expression %load loads files relative to notebook current working directory.
The working directory for every new Jupyter notebook is set once started the notebook server.
It's the same directory from where you started the server (if didn't specified a different one with the proper option jupyter notebook --notebook-dir=some_directory).
You can get or change the working directory of your notebook with some system magics (special notebook functions) respectively:
%pwd (print working directory)
%cd some_folder/some_other_folder (change directory)
Let say you just started the notebook, and your current directory is your home directory and thus the output of %pwd will be something like
/home/some_user.
If you want to load files from a specific folder, i.e. /home/some_user/my_code (or if you are using Windows C:\Users\Some_user\my_code) you can type
(each in a separate notebook cell):
%cd my_code
and then:
%load my_script1.py
%load my_script2.py