Running Jupyter Notebooks Locally from PowerShell or CMD

Because I’m lazy, I wanted to just click on a single file to startup Jupyter notebook on my local machine. At first, I tried writing a powershell script to do this, but received an error when trying to activate the env from within powershell.

To fix this, I discovered you have to enter the command “Conda init powershell” into the command prompt. This will make some adjustments to Anaconda. You then need to close down powershell and reopen it. After I did this, everything worked fine. My Script is:

cd first-steps-with-python
conda activate intro-to-python
jupyter notebook

But, I thought this was not good enough, because you have to load a powershell script first before it can be executed, unlike a bat file.

So I wrote a bat file to do the same thing, but then encountered the same problem of trying to activate a conda env from within a command line. Once again, I just entered “conda init cmd.exe” into the CMD command prompt to make any necessary adjustments, then closed and reopened the command prompt.

While this overcame the activate problem, I discovered my bat file was just terminating without doing anything meaningful. I think this was because I was just passing control to an anaconda bat file and the process was never returning to my bat file.

To get the bat file to work, you need to use the call command. In DOS, call will call another batch file and then return to the current batch file to continue processing.

So the commands that worked for me were:

cd c:\users[NAME]\first-steps-with-python
call activate intro-to-python
call jupyter notebook
pause

[NAME] needs to match the name of your user directory

Just enter these commands into a text file and save the file as LaunchJupyter.bat. Then double click to execute.

1 Like

You may directly activate conda environment by:

conda activate env_name

Or activate the base environment at Windows startup.
Reference:
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#conda-init