Edit the Jupyter terminal's starting directory - python

when I launch a Jupyter notebook (which I installed through Anaconda), the explorer opens in C:\Users\Joe
However, when I open a terminal (New -> Terminal) it opens in C:\Users\Joe\Documents
Is it possible to change the starting directory of the terminal, so it matches the notebook explorer page?
I have already tried the following:
1) edit the 'Start in' field in windows PowerShell properties to my desired location. This didn't change anything - it's as if when jupyter opens the terminal it overrides this - though it seems to use PowerShell.
2) running jupyter notebook –generate-config, and inserting my desired path to c.NotebookApp.notebook_dir = "".
Any advice would be appreciated, thank you!

Resolved this myself - found the answer here:
Find the default local Jupyter directory
In the Anconda3 folder there is a file cwp.py, which determines where the working directory should be changed to. I commented out lines 26-30:
if exception:
documents_folder, exception = get_folder_path(FOLDERID.PublicDocuments)
if not exception:
os.chdir(documents_folder)
and the path in the terminal is no longer being modified.

Related

VSCode loads the wrong python version in the interactive window

I am trying to use the Interactive window of VSCode, but it loads the wrong version of Python. I want it to load the version of the virtual environment (shown bottom left), but it defaults to something else.
I have tried this approach, but I can't simply click the version and change it because it isn't included in the drop down list. I have tried adjusting the settings.json file python.defaultInterpreterPath but that didn't work (perhaps my syntax is wrong). I have also made sure that jupyter is installed in the virtual environment.
I always run VSCode in the project folder from the command line using code . but strangely, something that has worked is running it using code (without the period). This is not a solution however, because the workspace folder is not updated, meaning that when I change the project I'm working on, VSCode opens in previous workspace.
You can try to delete all the files and folders under this location:
C:\Users\${UserName}\AppData\Roaming\Code\User\globalStorage
Others:
The python interpreter of the .ipynb file was stored in the database located at:
C:\Users\${UserName}\AppData\Roaming\Code\User\workspaceStorage
Such as:
If you open a new .ipynb file in the VSCode which name has not storaged in the database, you will need to select the interpreter.
It's different from the interactive. You can find, any time you reopen the VSCode, the last time you created interactive will be disappear.

Change Jupyter notebook working directory

When I open the notebook e.g. at home home / user / Desktop / multifit-master / notebooks / CLS-DE.ipynb.
And then type in the notebook (CLS-DE.ipynb) %cd I get:
home/user
Shouldn't this be the directory where I opened my notebook? It means :
home / user / Desktop / multifit-master / notebooks / CLS-DE.ipynb
How to make a notebook in the directory where I open it?
I want to see after typing %cd in CLS-DE.ipynb notebook this directory:
home / user / Desktop / multifit-master / notebooks / CLS-DE.ipynb
To answer your first question, %cd executes shell command cd, which means "go to home directory". Your home directory is /home/user.
If you want to change where jupyter saves notebooks:
Define it when starting jupyter jupyter notebook --notebook-dir=home/user/Desktop/multifit-master/notebooks for Jupyter or jupyter-lab --notebook-dir=home/user/Desktop/multifit-master/notebooks for JupyterLab.
Switch to the required directory before starting jupyter.
To get what you want, i.e. "I want to see after typing %cd in CLS-DE.ipynb notebook this directory", you'd have to change HOME environment variable. I'd never go this way, as number of tools, scripts, libraries etc. might refer to you HOME. Changing it is likely to bring unforeseen consequences, with little benefit.
option 1 :
open terminal, change path where ever you want to and then open jupyter notebook
via terminal.
jupyter notebook
Option 2 :
u can use os.chdir
In the terminal use cd to go in to the respective directory. Once there, type "jupyter notebook" in the terminal. This should open the notebook in that directory.
And to make sure that the notebook is open in the directory you wanted, type "%pwd". It means "print working directory".

Jupyter on Mac: run files from non-default directory

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.

Jupyter Notebook error. No such file or directory C:.../HOME

I have installed Anaconda2, and today decided to update to Anaconda3. I installed Anaconda3 at the same time as I uninstalled Anaconda2.
Now, Jupyter Notebook shows this error and quits every time I try to start it.
The path in the error message is: "C:\Users\Hongyi\Documents\%HOME%"
What is causing Jupyter Notebook to quit?
Okay it looks like I found a solution.
Jupyter started running after I created the appropriate folder in the appropriate path.
In my case, it is creating a folder named %HOME% (with percent signs), in the path shown in the error message.
Now Jupyter is up and running.
Thank you guys for your thoughts and suggestions.
After readed this solution I tried another one (a comfortable one for me):
Open the location of the "Jupyter Notebook" shortcut (right click > More > File location) and right click again > Properties. In the Destination field (Sorry if the label is another ... the thing is that my Windows is in Spanish and I don't remember how it's labeled this field in English) just change the %HOME% at the end for a path to your "Work Folder" (if you have not created one then just create it). And this way It works also.

jupyter notebook starting directory

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

Categories