Using a .bat to change directories and run Jupyter - python

I'm new to coding but I simply want to change directory and run jupyter. The problem is cmd instantly closes once it reaches the jupyter notebook command. Tried cmd /k too but it doesn't have an effect. I must being doing this wrong.
F:
cd directoryname
activate environmentname
jupyter notebook
pause
Solution:
The commands were closing the prompt for some reason when executed in a .bat (they don't when typed). The fix was to type call before the commands.
F:
cd directoryname
call activate environmentname
call jupyter notebook
pause

The commands were closing the prompt for some reason when executed in a .bat (they don't when typed). The fix was to type call before the commands.
F:
cd directoryname
call activate environmentname
call jupyter notebook
pause

Create a simple batch file (jnote.bat):
#echo off
call jupyter notebook "%CD%"
pause
In the same folder create a shortcut to the batch file and rename it to jupyter-notebook.
Open the shortcut properties and change the icon to the jupyter.ico. You will find this in the .\Menu sub-folder in your Anaconda distribution. You should now have a shortcut with the) nice jupyter icon.
Copy the shortcut to all the folders that you use for your notebooks.
Double-click the shortcut to open jupyter-notebook that folder.

For Jupyter, use:
%USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\cwp.py %USERPROFILE%\Anaconda3 %USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\Scripts\jupyter-notebook-script.py "**file location**"
for Jupyter lab:
%USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\cwp.py %USERPROFILE%\Anaconda3 %USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\Scripts\jupyter-lab-script.py %USERPROFILE%
Save it in a .bat file, with necessary changes at 'UserName' and 'File Location' . Keep it as a single line. Just double click the file to open jupyter notebook at the location.
Note: File Location is the location of the notebook to open.
Alternatively, you can use the same commands to create windows shortcut, without the need for the .bat file:
On the desktop, right-click -> new -> shortcut.
As the location of the item, paste the code above.
Then, you can double click the shortcut to launch jupyter. Unlike the bat file, you can also pin this shortcut to taskbar.
For details on how to setup jupyter lab, check out Running JupyterLab as a Desktop Application in Windows 10

Assuming activate and jupyter are executables or otherwise valid commands, everything should be okay. Since you're saying the cd command is probably the culprit, try the below:
Perhaps you are trying to change to a directory on a different drive... if this is the case you will need to use cd /d directoryname instead.
If this doesn't work, try putting a bunch of pause statements between each command to see exactly where it is breaking.

You can used just that simple code:
d: & activate your_env & jupyter notebook
It's really working for me

I found an alternative way to solve this problem without a .bat.
Search your start menu for "Jupyter Notebook". You should find a shortcut called:
"Jupyter Notebook (environmentname)"
This short cut was created when I set up my environment. In my case environmentname is py35.
To change the directory that Jupyter Notebook starts in find the "Anaconda Prompt" shortcut. Then open "Properties>Shortcut" and change the "Starts in:" field to your desired directory.

If you did not add conda to your path, the following script might be helpful for you. According to https://docs.anaconda.com/anaconda/install/multi-user/ you might even want to check C:\ProgramData\Anaconda but I didn't want to add something to the Batch script that I haven't checked myself.
:: Starts JupyterLab
SET CONDA_ENV=my-specific-conda-environment-name
where conda 2> nul
IF ERRORLEVEL 1 (
ECHO "Conda is not available in your PATH. Guessing the location of the installation..."
CALL C:\Users\%USERNAME%\Anaconda3\Scripts\activate %CONDA_ENV% || (
ECHO "Either Conda or the environment was not found."
PAUSE
)
) ELSE (
CALL activate %CONDA_ENV%
)
CALL jupyter lab || (
ECHO "JupyterLab encountered an error, please check the error message"
PAUSE
)

here is the batch file for this problem
cd directoryname
call activate envName
start jupyter notebook

This is what I use to activate my virtual environment, then run my notebook.
#echo off
call env\Scripts\activate
call jupyter-lab
Saved this as a .bat, so all I have to do to open the notebook is run it.

The best solution I found so far, which works without creating a custom batch file:
Navigate to activate.bat in C:\Users\<youruser>\<Anaconda3 or miniconda3>\Scripts\activate.bat
Right click and create a link to the activate.bat from the Desktop
Right click to change property of the link in the tab link
In Target you enter: %UserProfile%\<Anaconda or miniconda3>\Scripts\activate.bat <your-env-name> && #CALL jupyter lab (when you do not know your environment name you can check them in the file environments.txt in C:\Users\<youruser>\.conda
In the field below you enter your path to the project folder (not to the environment), this is where the script will start.
After this you can edit the icon / title / .. of the link and execute.

This is my code. It starts jupyter notebook in the directory it is saved:
#echo on
cd 'your_path_to_anaconda' /condabin
call activate.bat
cd %~dp0
call jupyter notebook
cmd \k

Related

Quickest way to open jupyter notebook

Every day, I repeat the same procedure over and over to open a Jupyter notebook.
I navigate with File Explorer to my work folder C:\Users\MYSELF\WORKFOLDER
Open command prompt there
Enter the command jupyter notebook or jupyter lab
If you don't have jupyter in your python base environment it becomes even more painful because you need to activate some virtual environment.
Is there a quicker way?
I'd love to have an icon on my desktop that I can just click on!
Right-click on your desktop New -> Shortcut. Then enter the following for the location:
C:\Windows\System32\cmd.exe /k cd C:\Users\MYSELF\WORKFOLDER && jupyter notebook
This will start a command prompt and execute the two commands cd C:\Users\MYSELF\WORKFOLDER and jupyter notebook.
Click Next and you can then give that shortcut a name and create it with a click on Finish.

Open .ipynb in Anaconda Jupyter Notebook on double-click (macOS)

I've been trying to figure out how I can open a .ipynb file on double-click on a Mac, so the file opens with Anaconda and then automatically opens Jupyter Notebook with the file contents.
I have tried creating an application through Automator that opens with a jupyter_lab command, but the issue is that Jupyter lives in Anaconda, so this method is not working. See these articles for my steps: https://samedwardes.com/2020-01-31-open-ipynb-with-double-click/ and Open an ipython notebook via double-click on osx
I have also tried installing nbopen with the following:
python3 -m pip install nbopen
This works, and then I can run nbopen file.ipynb through the terminal, but I want to be able to bypass this step and do it while in file explorer, physically looking at the file instead.
I have ran the recommended command for osx to integrate it with file manager ./osx-install.sh but I just get zsh no such file or directory and can't really find any help with figuring out why this is pushing back an error.
Any suggestions?
I have this "convenience issue" as well, and didn't go for the nbopen route, but instead, I just made a batch script (I named it jupyter-notebook.bat) that calls certain conda functions, and pretty much initiates things like how double clicking works. In the script, I just have this:
call "C:\Users\XYZ\Anaconda3\Scripts\activate.bat"
call conda activate myEnvironment
call python C:\Users\XYZ\Anaconda3\Scripts\jupyter-notebook-script.py %1
Lastly you just need to configure that every .ipynb opens up using your jupyter-notebook.bat script.
For consistency, I placed the script in my Anaconda folder. And if you also have nb_conda_kernels installed in your base environment, you'd have access to the other environment you've created as well from there.

Jupyter Notebook : 'Bad config encountered during initialization. No such notebook dir : D:/ABC'

I run Windows10, use the latest Anaconda3 (2019) and have installed Jupyter Notebook through conda.
I used to run Jupyter Notebook by navigating to a folder where I save my Jupyter Notebooks named \jupyter_notebook_files and from that directory running the command jupyter notebook from cmd.
A few hours ago I deleted for space saving reasons a folder named ABC.
When now I try to run Jupyter Notebook I get the message:
[C 20:57:04.610 NotebookApp] Bad config encountered during initialization:
[C 20:57:04.611 NotebookApp] No such notebook dir: ''D:/ABC''
I dealed with this problem by typing:
C:\WINDOWS\system32>jupyter notebook --notebook-dir='D:\DIGITAL_LIBRARY\Jupyter_Notebook_Files'
Is there a way to make this folder default and not have to repeat each time the clause: -notebook-dir='D:\DIGITAL_LIBRARY\Jupyter_Notebook_Files' ?
Try to run this code on the terminal:
jupyter --config-dir
The output will be a path. Go to that folder and delete all the contents.
Then run
jupyter notebook
Close Anaconda and or any related programs that are running (Presumably Jupyter is not). Drop down to the command prompt in Windows 10 (or other terminal if you have a different OS). Enter the command:
jupyter --config-dir
This will give you the configuration directory for Jupyter, which will most likely look something like this:
C:\Users<Your Local Account>.jupyter
use CD to go to that directory and use Notepad or your favorite text editor to edit the file called "jupyter_notebook_config.py" - for example:
notepad jupyter_notebook_config.py
Now use find (F3) to search for the line that begins with "c.NotebookApp.notebook_dir = ". The directory that follows the "=" sign probably doesn't exist. So copy that and create the directory OR enter the directory you want instead.
Save and Start Jupyter.

Running Anaconda environment from cmd Python

When I open the anaconda prompt the window that opens, shows:
(base) C:\Users\sherv>
So I deactivate it and get:
C:\Users\sherv>
which is the same thing when I open cmd. From here there is there any way that I can reactivate conda? Because the command words like "conda -v" or "activate base", etc, don't work.
Also, the conda prompt is a shortcut so when I right-click and select "open file location", it goes to windows\system32\cmd. So I don't understand why I can't activate conda environments from cmd if it's the same thing?
This is the anaconda prompt shortcut:
When I open file location its the cmd:
I have even tried to add it to that path just in case it might work?
PS. I'm very new to all this and trying to connect some dots; sorry if it's a stupid question.
ok, so the path first opens the cmd (which is why the shortcut points at it, but then from there, from within the cmd, it runs C:\Users\sherv\Anaconda3\Scripts\activate.bat. Is there anyway I can have the batch file commands run from cmd without having to write out the path every time? I added the path for the batch file but it didn't work
The reason why the anaconda prompt shortcut leads to cmd is because it needs to open cmd first and then run a batch file which activates the conda environment. What I had done was to add the batch file to the path but I wasn't using the correct commands to run the conda environment. Therefore, all the keywords such as activate etc didn't work.
Simply type activate.bat and the file which is added to the path is opened which takes you to the base conda environment.
You can use this method to run different programs on different APIs and export the result back and analyse it.
Thanks for downvoting my question :)
I figured out how to do it, in my case with miniconda so it might be slightly different.
Find conda.exe in the Scripts folder. For me it was C:\Users\[SomeUser]\miniconda3\Scripts\conda.exe
Open command prompt and navigate to that same directory
Run conda init cmd.exe - To see more info on this command, you can run conda init --help, where it says "Initialize conda for shell interaction. [Experimental]"
Now after reloading command prompt, from anywhere you should be able to do conda activate WhateverEnvName and it will start it in that environment
If you want to make a batch file that opens to the environment, you can use cmd.exe /k conda activate WhateverEnvName
Alternatively, if you don't want to just open the environment, but actually want to launch a python script directly using that environment, you can type the full path to the python.exe in that environment folder, and then the relative path of the python script to run with that environment.
For example, say you open the command prompt in some arbitrary directory that contains "myscript.py" that you want to run with an environment called "whateverEnv".
You'd do: C:\Users\SomeUserName\miniconda3\envs\whateverEnv\python.exe myscript.py
You could also put that same line into a batch file.

double click to open an ipython notebook

Related posts: Open an ipython notebook via double-click on osx
How can I open Ipython notebook from double click? I always cd to the directory of the notebook, then type ipython notebook in the browser and then open it in the browser. This steps is very inconvenient.
In windows, I remembered that I can change the directory in the browser, but in linux there is no way to explore to other directory via browser, if I want to open a new book in another directory, I have to restart another kernel as above, which annoys me.
Is there any simple and verified way to do this?
You can use a project like nbopen that handle that and will open the browser on the right notebook + start an IPython server if one is not yet running.
pip install nbopen.
open Automator, create new Application
Drag'n drop Run Shell Script
Change Pass input to as arguments
Copy/paste this script:
variable="'$1'"
the_script='tell application "terminal" to do script "nbopen '
osascript -e "${the_script}${variable}\""
Save the new application to Applications directory as nb_open
Right click any ipynb file and select "Open with > Other" and select the nb_open in the Applications folder. Don't forget to check "Always Open With".
Select an ipynb file, get info (command + i) > Open With (select nb_open if not selected already) > Click Change All.... Done.
To "promote" Yogesh's helpful comment to a fully self-contained answer:
Windows 10
In a CMD or PowerShell window with administrative rights (e.g. launched quickly with Win+X, then A), run:
pip install nbopen
python -m nbopen.install_win
Profit!
Double-click on *.ipynb files now starts a new server or reuses an existing instance.
One way to open a Jupyter notebook directly by double-clicking on the file is to associate the .ipynb file extension with the jupyter-notebook command. Here's how to do it on a Windows system:
Right-click on the Jupyter notebook file that you want to open.
Select "Open with" and then choose "Choose another app".
Select "More apps" and then scroll down to the bottom and choose "Look for another app on this PC".
Navigate to the directory where the jupyter-notebook.exe file is located (usually in the Scripts subdirectory of your Python installation), and select it.
Check the box next to "Always use this app to open .ipynb files" and then click on "OK".
Now, when you double-click on an IPython notebook file, it should open directly in the Jupyter notebook.
On a Mac or Linux system, you can set the default application for .ipynb files by using the xdg-mime command. First, determine the full path to the jupyter-notebook executable:
which jupyter-notebook
This will return the path to the executable. Then, use the xdg-mime command to set the default application for .ipynb files:
xdg-mime default jupyter-notebook.desktop application/x-ipynb+json
Replace jupyter-notebook.desktop with the path to the jupyter-notebook executable that you determined earlier. Now, when you double-click on an IPython notebook file, it should open directly in the Jupyter notebook.

Categories

Resources