20

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
Jubei
  • 301
  • 1
  • 2
  • 7
  • 1
    Please accept your own answer that you've given below, as it is your accepted solution (and it also helped me :) ) – tonysepia May 11 '19 at 08:44
  • This helped me too, after having installed jupyter with "pip install jupyter". I think if you install it as part of a package (e.g. conda) other answers are relevant. – Levin Magruder Aug 07 '21 at 00:17

11 Answers11

10

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
Jubei
  • 301
  • 1
  • 2
  • 7
  • Reason: `activate` and `jupyter` are batchfiles (`activate.bat` and `jupyter.bat`). Just executing them transfers control to them and when they are finished, the parser is done (and the window closes when you just doubleclicked your first script). `Call` returns control to the original script, when the called script ends. – Stephan Oct 18 '20 at 08:21
4

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:

  1. On the desktop, right-click -> new -> shortcut.
  2. 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

BoZenKhaa
  • 782
  • 1
  • 6
  • 22
GokulDAS027
  • 714
  • 1
  • 10
  • 22
  • 1
    Genius. I've also found that by replacing `jupyter-notebook-script.py` by `jupyter-lab-script.py` you can run Jupyter Lab instead if desired. – ffi Aug 31 '20 at 08:16
4

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.

Steve Jones
  • 141
  • 1
  • 5
3

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.

Erik Quinn
  • 46
  • 3
  • Pauses between and after all commands results in cmd instantly closing after `activate environmentname` runs (it skips the post command pause) However either `cmd /k activate environment` or `cmd /k jupyter notebook` properly runs either command. But I cannot run both commands using this method. – Jubei Feb 21 '16 at 22:05
3

You can used just that simple code:

d: & activate your_env & jupyter notebook

It's really working for me

  • This is the only thing that work for me!!! When using jupyter-lab, all other options with the "call" and "pause" command shut down the server immediately and I got "Server connection error" ... Connection could not be establisehd. – J.T. May 31 '22 at 14:09
1

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.

Dustin Helliwell
  • 630
  • 1
  • 8
  • 12
0

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
)

mkastner
  • 103
  • 10
0

here is the batch file for this problem

cd directoryname
call activate envName
start jupyter notebook
ajayramesh
  • 3,576
  • 8
  • 50
  • 75
0

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.

ibuz
  • 21
  • 3
0

The best solution I found so far, which works without creating a custom batch file:

  1. Navigate to activate.bat in C:\Users\<youruser>\<Anaconda3 or miniconda3>\Scripts\activate.bat

  2. Right click and create a link to the activate.bat from the Desktop

  3. Right click to change property of the link in the tab link

  4. 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

  5. In the field below you enter your path to the project folder (not to the environment), this is where the script will start.

  6. After this you can edit the icon / title / .. of the link and execute.

rokdd
  • 541
  • 4
  • 14
-1

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
JoSSte
  • 2,953
  • 6
  • 34
  • 54
ezque
  • 1