Launch Anaconda Jupyter Notebook Environment from Any Folder in Any Drive
saintniyi
Posted on April 19, 2022
The Problem
I recently installed Anaconda on my machine. But every time I tried opening my existing Jupyter notebooks from the Anaconda navigator, it always ended up opening a new notebook that defaulted to drive C. Noticing this, I tried using the navigator to open my existing notebooks from a specified folder, no luck yet.
I decided to give the Anacoda prompt a try, whoa and it works. And also, does not take long to figure out as well.
Although, I could do this through a series of commands on the Anaconda prompt, imagine what it would be like to do this with a single command?
cooooool right!
I set out researching on this and saw a lot of great suggestions on how to get this done.
Out of all the results, of particular interest to me was the method of using the command - jupyter notebook --generate-config
- on the Anaconda prompt.
After spending what seemed like an eternity; diligently following all the instructions I could lay my hands on, for this method and several other suggestions, doing it all over again to ensure I was doing it all well, I still was unable to make it work.
Oh yes, I was frustrated and angry and this led me to decide to find a solution that is guaranteed to work for anyone who follows this method on a Windows O/S platform.
Eventually, after some couple of hours playing with the Anaconda prompt and the installation folder, I was able to come up with a beautiful solution and I thought I'd share it with you all and protect you from going through the same torture I went through.
I hope it will be of help to you. Happy reading!
How Can We Solve This?
Open Jupyter notebobook in specified Anaconda Environment from any folder through the Anaconda Prompt
Interestingly, opening Jupyter notebook from any folder through the anaconda prompt is fairly easy, if one has little knowledge of DOS commands and Anaconda prompt commands.
But here is how you can open a Jupyter notebook in a specified folder through the Anaconda prompt:
Type Anaconda in the search box of your Windows taskbar, this will bring up some search results related to your search term of which the Anaconda Prompt will be a part of.
Go ahead and select Anaconda Prompt from the results displayed.Change to the folder path where you would like to have your new or existing Jupyter notebook(s).
e.g if my Jupyter notebook will be in, or is already inD:\Lessons\PythonProjects
, at the prompt, type:
d:
followed with Enter ↵
Then typecd d:\Lessons\PythonProjects
also followed with Enter ↵Assuming you have an environment with the name: my-env previously setup in your Anaconda environment. To ensure your notebook opens up in the environment; at the prompt, type the command
conda-activate my-env
Enter ↵.
Note the word my-env showing at the start of the prompt.Finally, launch notebook by using the commands
jupyter notebook
orstart jupyter notebook
at the prompt e.g.jupyter notebook
Enter ↵.
Sweet as this may look, I soon got weary of having to always type bunch of commands to launch my notebook. So I decided to make it further sweeter.
Open Jupyter notebook in specified environment from any folder with a Batch file
To achieve this, we will need to use a batch file that automates the method described above.
First Step
- Type Anaconda in the search box of your Windows taskbar, this should return a list of menu items, from these lists, select Anaconda Prompt to open it.
- At the Anaconda Prompt, type:
where conda
Enter ↵. This displays the list of installation paths as well as the drive for the Anaconda executable.
(base) C:\Users\Test>where conda
C:\Anaconda3\Library\bin\conda.bat
C:\Anaconda3\Scripts\conda.exe
C:\Anaconda3\condabin\conda.bat
(base) C:\Users\Test>
Note: My drive in the screenshot is C:
- Open a notepad and type the commands below in it.
@echo off
cd\
c:
cd c:\Anaconda3\condabin
CALL conda.bat activate my-env
d:
cd d:\Lessons\PythonProjects
jupyter notebook
@echo on
Oh yea, that may seem a tad confusing but do not fret, I will explain each line and what it means
Explanation for the commands above
- The first command
@echo off
is optional. You can omit it if you like. It is meant to turn off display of command execution on the screen. - The command
cd\
takes your anaconda prompt to the root drive. From here we can change to any drive letter that contains our anaconda drive. - The next command
c:
is to ensure it uses the root drive of my Anaconda installation path shown in step 2 since my root drive isc:
. In your case, use the drive letter shown in your step 2. This is important in case your root drive is not the same as your anaconda installation path. - The command
cd c:\Anaconda3\condabin
, will change the anaconda prompt path tocondabin
. This is one of the path displayed in step 2. Ensure you change to thecondabin
of your machine, as shown in your step 2. - The command
CALL conda.bat activate my-env
changes the anaconda prompt from the base environment to the specified my-env environment as shown in the command. It is a standard practice to create an environment in anaconda for your work rather using the base environment. In case you want to know how to create your environment, at the anaconda prompt type:conda create --name test-env
Enter ↵ to create a test-env, then type:activate test-env
Enter ↵ to make it ready for usage. - The next command
d:
changes my anaconda prompt to drive d. This is the drive containing my existing notebooks. In your case, use the drive letter on your machine that you wish to have your notebook(s) or that contains your existing notebook(s). - The command
cd d:\Lessons\PythonProjects
changes my anaconda prompt path to pythonprojects folder, which contains my python notebooks. In your case, use any folder in the drive of your choice. - The
jupyter notebook
orstart jupyter notebook
command will launch jupyter notebook in the folder you specified. - The last command
@echo on
is optional. You can omit it if you like. It is meant to turn on display on our screen.
Second Step
- Save the file in your desktop with the name: RunJupyterNotebook.bat Ensure in the "Save as type:" section, you select "All files" in the drop down. After saving, close the notepad and observe the "RunJupyterNotebook.bat file on the desktop.
- Double-clicking this file will open your jupyter notebook environment in the specified folder and your anaconda command prompt.
Launch Jupyter notebook in specified environment from The Start Menu
Now that we have been able to launch it from the desktop, we can even take it a step further by launching it from the Windows start menu. To do this, we need to pin it to the start menu.
Ordinarily in Windows 10, when we right-click a program, we see the option to pin to start. However, this is not available for our batch file.
To achieve this, we do the following:
Right-click the RunJupyterNotebook.bat file on the desktop and select Desktop (create shortcut). This creates the RunJupyterNotebook - Shortcut.
Open any folder and in the folder address bar, paste the command
%appdata%\Microsoft\Windows\Start Menu
Enter ↩ as shown below:
Cut the RunJupyterNotebook - Shortcut and paste in the program folder that was opened in the step above.
Right-click the shortcut in the program folder and select properties.
A dialog box like the one below pops out.
In the dialog box that pops out, under the target field, type the command -
cmd /c
in front of whatever is in your target field as shown below:
Click the OK button
Observe the RunJupyterNotebook - Shortcut icon assume the resemblance of command prompt as soon as you click the OK button.
Finally, right-click the RunJupyterNotebook - Shortcut icon and you will see the Pin to start* menu item.
Select it and voila, a menu for launching your jupyter notebook in your specified environment from any folder will be pinned to your Start Menu as shown below:
I hope you found reading this insightful and hopefully it solves any issue you may encounter while using Anaconda Jupyter notebook. Also, don't hesitate to reach out if you have questions or just want to chat about it.
Posted on April 19, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.