avatarLeAnne Chan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3511

Abstract

n your browser.</p><figure id="b1c0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nrH6Ie7yCmhW4JQA6blVhg.png"><figcaption>Terminal command to launch Jupyter Notebook</figcaption></figure><p id="6e2e">Your browser will have a page like this. Then select ‘New’ from the top right hand corner and start coding!</p><figure id="c1b1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-d9jULv_HV5Vbyl5YnKtJQ.png"><figcaption></figcaption></figure><p id="ff2e">This is alright… but there are too many irrelevant folders! The file or folder you want to access is probably buried somewhere inside Desktop or Documents or something. I mean, you <i>could </i>still access the folder you want from here, but wouldn’t it be neat if you could directly see the file you want, or go to the folder you want to save the new notebook in?</p><h2 id="d0b9">Step 3B: Alternative way of launching Jupyter Notebook</h2><p id="46d8">If you’ve done step 3, go ahead and terminate your current Terminal session. Open Terminal again and this time, type in <code>jupyter notebook pathToLocation, </code>where <i>PathToLocation</i> is a path like in the screen shot below. Note: You do not need to install Notebook as done in step 2 again.</p><figure id="9394"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*gAc61ZPGtHAUmOGkhTl6hw.png"><figcaption>Terminal Command for launching Jupyter in a specific directory.</figcaption></figure><p id="c57f">Yikes!! It looks super long and messy. Please don’t try to type out the path! In Mac it is simple. Just <b>drag and drop the folder you want into Terminal</b> and it automatically types out the path for you. Press enter, and the following page will open in your browser. Now you’ve got quick access to all the relevant files!</p><figure id="baf0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*4RzGu4JkI9AmvmZzyIuCew.png"><figcaption>What you see in your browser when launching Jupyter Notebook with a folder path.</figcaption></figure><p id="1c1e">This small extra step goes a long way in staying organised!</p><h1 id="1132">Part 2: Using your own Environment</h1><p id="dbcf">We can use different <a href="https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/environments.html">conda environments</a> every time we launch Jupyter Notebook. Using an environment.yml file allows you to easily install the packages you need when launching Jupyter. This is what an environment.yml file looks like.</p><figure id="31d0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*iW9k94HYfdOdQdua5RVS4Q.png"><figcaption>An environment.yml file in Sublime Text Editor</figcaption></figure><p id="94fb"><a href="https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/channels.html"><b>Channel</b>s</a>: Locations where packages are stored. <b>Dependencies</b>: Python packages.</p><p id="7ca0"><b>WHY do this? </b>An environment.yml file is a collection of these python packages. I think having different environment created allows you to switch easily between different versions of packages for different projects. They also make it easier for there to be standardised environment across different team members. I welcome comments on other reasons why we do this because I myself am not 100% sure.</p><p id="f528">If you have an environment.yml file (always make sure it has a .yml extension and not .txt), we can now move on to how to use the environment.</p><h2 id="ed36">Step 1: Create env

Options

ironment in Terminal</h2><p id="02fd">Open Terminal again, and after the $, type: <code>conda env create --file pathToFile --name whateverNameYouWant</code> . Be very careful to have the space before the double dashes and no space right after the double dashes. To get the <i>pathToFile</i>, simply <b>drag and drop the environment file</b> from finder into Terminal after typing in <code>--file</code> (<b>don’t forget the space</b> after file before dropping in the file!). Ensure your <i>whateverNameYouWant</i> has no spaces.</p><figure id="69b8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Y3yNxGZ7BPYESNRa_mIjCA.png"><figcaption>Terminal Commands or creating your environment</figcaption></figure><figure id="e53c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*d4OJiiO8s8UySaD_FuPePg.png"><figcaption>Success in creating environment!</figcaption></figure><h2 id="2356">Step 2: Activate the environment</h2><p id="85a4">Before launching Jupyter Notebook, you have to <i>activate</i> the environment you wish to use. (Note: You only need to create the environment once, and when you open Terminal subsequently you can jump right to this step.) Type in <code>conda activate nameOfEnvironment</code>, then enter, and you see that what is in the brackets changes. <i>nameOfEnvironment </i>is the name you chose when creating the environment in Step 1. In that new line, repeat part 1 step 3B to launch Jupyter Notebook.</p><figure id="e092"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UNxQcCR-Uus_vWruvinQvQ.png"><figcaption>Terminal code for activating environment and then launching Jupyter Notebook.</figcaption></figure><p id="a3b5">Congrats, you now have launched Jupyter Notebook with specific packages.</p><h2 id="053a">Step 3: Updating your Environment</h2><p id="7a4e">Sometimes you want to change a few packages in your existing environment, but not create a new environment. I very stupidly created new environments every week in one semester when I could just have been updating the existing environment, and it was <i>soooo </i>messy.</p><p id="a7f2">To avoid my mistake, update your environment using the following command: <code>conda env update --name nameOfEnvironmentYouWantToUpdate --file pathToNewEnvironment</code> . Once again, simply drag and drop the new environment file into Terminal after <code>--file</code> .</p><figure id="ba1b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bAx6L_uwdkigCmFYJHZwPw.png"><figcaption>Command for updating existing Environment in Terminal</figcaption></figure><p id="bbc6">Now just repeat step 2 to launch Jupyter Notebook with your updated environment!</p><p id="1ed1"><b>That’s it! I hope this is useful to my future self as well as anyone who is new to Jupyter Notebook. Please let me know if I’ve made a mistake or missed out any steps! Thank you!</b></p><p id="74ec">Acknowledgements: Thank you to Nick Hand, professor of the MUSA (Master of Urban Spatial Analytics) 620 course — Geospatial Data Science in Python, who taught me how to program in Python in the Fall 2019.</p><p id="f2c7">This article by <a href="https://towardsdatascience.com/@msarmi9?source=post_page-----bc6180fc533----------------------">Matthew Sarmiento</a> on an in-depth explanation on conda environments. <a href="https://towardsdatascience.com/a-guide-to-conda-environments-bc6180fc533">https://towardsdatascience.com/a-guide-to-conda-environments-bc6180fc533</a></p></article></body>

A guide to installing Jupyter Notebook and creating your own conda environment in Mac

Hello! This is going to (hopefully) be a part of short articles on things I’ve learnt to do so far. This is kind of a revision for me as well as a documentation of steps for future reference. Hopefully it can also be useful to whoever may need it.

What is Jupyter Notebook? It’s a neat way for you to write code, run the code, and immediately see the results. In technical terms, its an Integrated Development Environment (IDE). There are a ton of awesome other IDEs out there and honestly, use whatever you’re most comfortable in or whatever your school or workplace is using. In Jupyter Notebook, you can select the option to create a Markdown. This will allow you to add in HTML elements within code chunks. You can use all the normal HTML tags like header tags (e.g <h5>) , underline (<u> ) or colour (<font color='#369c0'> ). Creating a Markdown is not unique to Jupyter. This is also possible in IDEs like R-Studio and Google Colab. Jupyter Notebook also allows you to download your work as a PDF, HTML, iPython Notebook (.ipynb), a python script (.py), markdown (.md) and a few other file formats, making sharing easy.

Jupyter: https://jupyter.org/index.html

Jupyter Notebook Markdowns: Write text within code chunks.

Part 1: Installing Jupyter Notebook

Step 1: Install Anaconda on your Mac

Python is prereq for using Jupyter Notebook. Downloading Anaconda will install python and many popular and useful python packages (these are packages created with methods that make programming much easier. Like numpy!) on you computer.

Download here: https://www.anaconda.com/products/individual

Step 2: Install Notebook in Terminal

Open terminal on your mac. I like to do this using Mac’s Spotlight Search and just typing in Terminal — simple, haha. The shortcut for opening Spotlight Search is command+spacebar. Search for Terminal, enter, and you should see the app.

Terminal!

Now type in pip install notebook into your terminal right after the $. pip is the package installer for python. This package is downloaded when you install Anaconda. install is one of the methods of pip. notebookthen refers to Jupyter Notebook. Press enter and wait until it has successful installed. You’ll know its done when you see the same last line you saw when you first opened Terminal. Something similar to this (base) Leannes-MBP:~ leannechannie$ and a blinking cursor.

Step 3: Launch Notebook!

We’re basically almost done! Simply type in jupyter notebook in terminal after the $ and that will launch Jupyter Notebook in your browser.

Terminal command to launch Jupyter Notebook

Your browser will have a page like this. Then select ‘New’ from the top right hand corner and start coding!

This is alright… but there are too many irrelevant folders! The file or folder you want to access is probably buried somewhere inside Desktop or Documents or something. I mean, you could still access the folder you want from here, but wouldn’t it be neat if you could directly see the file you want, or go to the folder you want to save the new notebook in?

Step 3B: Alternative way of launching Jupyter Notebook

If you’ve done step 3, go ahead and terminate your current Terminal session. Open Terminal again and this time, type in jupyter notebook pathToLocation, where PathToLocation is a path like in the screen shot below. Note: You do not need to install Notebook as done in step 2 again.

Terminal Command for launching Jupyter in a specific directory.

Yikes!! It looks super long and messy. Please don’t try to type out the path! In Mac it is simple. Just drag and drop the folder you want into Terminal and it automatically types out the path for you. Press enter, and the following page will open in your browser. Now you’ve got quick access to all the relevant files!

What you see in your browser when launching Jupyter Notebook with a folder path.

This small extra step goes a long way in staying organised!

Part 2: Using your own Environment

We can use different conda environments every time we launch Jupyter Notebook. Using an environment.yml file allows you to easily install the packages you need when launching Jupyter. This is what an environment.yml file looks like.

An environment.yml file in Sublime Text Editor

Channels: Locations where packages are stored. Dependencies: Python packages.

WHY do this? An environment.yml file is a collection of these python packages. I think having different environment created allows you to switch easily between different versions of packages for different projects. They also make it easier for there to be standardised environment across different team members. I welcome comments on other reasons why we do this because I myself am not 100% sure.

If you have an environment.yml file (always make sure it has a .yml extension and not .txt), we can now move on to how to use the environment.

Step 1: Create environment in Terminal

Open Terminal again, and after the $, type: conda env create --file pathToFile --name whateverNameYouWant . Be very careful to have the space before the double dashes and no space right after the double dashes. To get the pathToFile, simply drag and drop the environment file from finder into Terminal after typing in --file (don’t forget the space after file before dropping in the file!). Ensure your whateverNameYouWant has no spaces.

Terminal Commands or creating your environment
Success in creating environment!

Step 2: Activate the environment

Before launching Jupyter Notebook, you have to activate the environment you wish to use. (Note: You only need to create the environment once, and when you open Terminal subsequently you can jump right to this step.) Type in conda activate nameOfEnvironment, then enter, and you see that what is in the brackets changes. nameOfEnvironment is the name you chose when creating the environment in Step 1. In that new line, repeat part 1 step 3B to launch Jupyter Notebook.

Terminal code for activating environment and then launching Jupyter Notebook.

Congrats, you now have launched Jupyter Notebook with specific packages.

Step 3: Updating your Environment

Sometimes you want to change a few packages in your existing environment, but not create a new environment. I very stupidly created new environments every week in one semester when I could just have been updating the existing environment, and it was soooo messy.

To avoid my mistake, update your environment using the following command: conda env update --name nameOfEnvironmentYouWantToUpdate --file pathToNewEnvironment . Once again, simply drag and drop the new environment file into Terminal after --file .

Command for updating existing Environment in Terminal

Now just repeat step 2 to launch Jupyter Notebook with your updated environment!

That’s it! I hope this is useful to my future self as well as anyone who is new to Jupyter Notebook. Please let me know if I’ve made a mistake or missed out any steps! Thank you!

Acknowledgements: Thank you to Nick Hand, professor of the MUSA (Master of Urban Spatial Analytics) 620 course — Geospatial Data Science in Python, who taught me how to program in Python in the Fall 2019.

This article by Matthew Sarmiento on an in-depth explanation on conda environments. https://towardsdatascience.com/a-guide-to-conda-environments-bc6180fc533

Jupyter Notebook
Python
Programming
Recommended from ReadMedium