Launch Anaconda Jupyter Notebook Environment from Any Folder in Any Drive
Tags: jupyter
Published: 2022-04-19 18:42:16

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

![Frustrated Person](https://res.cloudinary.com/moerayo/image/upload/v1643875395/frustrated1_xaawbi.gif)

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?
![solution picture](https://res.cloudinary.com/moerayo/image/upload/v1643875613/solution_ldgnjz.jpg)
###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:
1. 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.![Search Anaconda Prompt](https://res.cloudinary.com/moerayo/image/upload/v1642629668/searchanacondaprompt_um1jxf.png)

2. 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 in `D:\Lessons\PythonProjects`, at the prompt, type:
  `d:` followed with <kbd>Enter ↵</kbd>
   Then type `cd d:\Lessons\PythonProjects` also followed with <kbd>Enter ↵</kbd>

3. 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` <kbd>Enter ↵</kbd>.
**Note the word _my-env_ showing at the start of the prompt.**

4. Finally, launch notebook by using the commands `jupyter notebook` or `start jupyter notebook` at the prompt e.g. `jupyter notebook` <kbd>Enter ↵</kbd>.

_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
1. 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.
![Search Anaconda Prompt](https://res.cloudinary.com/moerayo/image/upload/v1642629668/searchanacondaprompt_um1jxf.png)
2. At the _Anaconda Prompt_, type: `where conda` <kbd>Enter ↵</kbd>. 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:`
3. 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
```
  ![confused person](https://res.cloudinary.com/moerayo/image/upload/v1643876041/confused_rnm9un.gif)

  _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 is `c:`.
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 to `condabin`.
This is one of the path displayed in step 2. Ensure you change to the `condabin` 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`<kbd>Enter ↵</kbd> to create a test-env, then type: `activate test-env` <kbd>Enter ↵</kbd> 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` or `start 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
1. Save the file in your desktop with the name: _RunJupyterNotebook.bat_
![Save Batch](https://res.cloudinary.com/moerayo/image/upload/v1643386528/savebatch_kpwkoq.jpg)
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.
2. Double-clicking this file will open your jupyter notebook environment in the specified folder and your anaconda command prompt.
![jupyter notebook anacodaprompt](https://res.cloudinary.com/moerayo/image/upload/v1643386958/jupyternotebook_anacodaprompt_rw5nxp.png)

###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_.
![RunJupyterNotebook Shortcut](https://res.cloudinary.com/moerayo/image/upload/v1643752070/RunJupyterNotebook_shortcut_jh8pqx.png)

- Open any folder and in the folder address bar, paste the command `%appdata%\Microsoft\Windows\Start Menu`<kbd>Enter ↩</kbd> as shown below:
![start program folder](https://res.cloudinary.com/moerayo/image/upload/v1643752101/startprogramfolder_kfmyen.png)

- 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.
![shortcut properties dialog](https://res.cloudinary.com/moerayo/image/upload/v1643752055/shortcut_properties_dialog_udypw7.png)

- 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:
![dialog box cmd](https://res.cloudinary.com/moerayo/image/upload/v1643752055/dialogboxcmd_ztfqo2.png)

- 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. 
![pin to start](https://res.cloudinary.com/moerayo/image/upload/v1643752168/pintostart_hnnyqj.png)
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:
![start screen launch](https://res.cloudinary.com/moerayo/image/upload/v1643753204/startscreenlaunch_llewpc.png)

Yes! We did it!
![Hurray](https://res.cloudinary.com/moerayo/image/upload/v1643876408/yay_xadgby.gif)

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