Lesson 6. Setup Your Earth Analytics Working Directory


Set Up Your Working Directory

As you learned in the previous tutorial, project organization is integral to efficient research. In this tutorial, you will create the project directory that you will use for all of your work. This project directory will be carefully organized with a \data directory that you will use to save all of the data you use in your lessons.

Learning Objectives

At the end of this activity, you will be able to:

  • Create an easy to use and well structured project structure.
  • Set a working directory using the Jupyter Notebook interface using os.chdir.

What You Need

You will need the most current version of Jupyter Notebook and earth-analytics-python environment loaded on your computer to complete this lesson.

Create earth-analytics Project Directory

Now that you have the basics of good project structure out of the way, let’s get your project set up. You are going to create an earth-analytics project directory (or folder) where you will store data and files used in the class. You will then set that project directory as your working directory in python using the syntax.

os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

Data Tip: Directory vs Folder: You can think of a directory as a folder. However the term directory considers the relationship between that folder and the folders within it and around it (it’s full path).

Follow the steps below to create an earth-analytics project directory on your computer and then a data directory located within that project directory. The steps below use bash to create your directory. YOu could also create this manually using File Explorer on a Mac or windows explorer on Windows.

  • Navigate to the home directory on your computer. This is likely a directory that ends with our username like:

On a MAC: /Users/yourUserName On a PC: `/c/Users/yourUserName

# navigate to your home directory
cd
  • In the directory, create a NEW DIRECTORY called earth-analytics.
mkdir -p earth-analytics/data

Above the -p argument makes a directory recursively - this means it will make both the earth-analytics and the data directory within it.

Data Tip: Notice that you are creating an easy to read directory name. The name has no spaces and uses all lower case to support machine reading down the road. Sometimes this format of naming using dashes is referred to as a slug.

working directory screenshot
Your project directory should look like this. Right now it just contains one directory called data.

Once you have setup your working directory, you are ready to test things out in Jupyter. To begin, activate the earth-analytics-python environment on your computer and launch jupyter notebook.

Then import the os and earthpy python packages.

import os
import earthpy as et

Finally, you can use the function below to set your working directory to your home/earth-analytics directory.

os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

The function below uses several functions

  1. os.chdir() is a function in the os package that is used to change your working directory. If you want you could manually add a path in quotes to the function to change your directory like this:

os.chdir("path/to/dir/here")

  1. the os.path.join() function allows you to build a path from a set of strings. This is a useful function because the path structure can vary on different operating systems. For instance the / slash is used on a mac whereas often windows uses \. By using os.path.join your code will work on any machine that has the directory structure that you list in the function. Thus your work is more reproducible.
os.path.join("look","im","making","a","path")
'look/im/making/a/path'

et.io.HOME is a function build to automagically find the home directory on your computer. Below you see the home for the computer where this notebook lives is:

/Users/lewa8222

et.io.HOME
'/Users/lewa8222'

So this code: os.path.join(et.io.HOME, 'earth-analytics') creates the full path and os.chdir() tells jupyter to change the working directory for the notebook to that path.

Make sure that you have an earth-analytics directory setup on your computer in the home directory. If you don’t have this setup, then it is possible that your instructor may not be able to run your code without editing it. Your instructor will not edit your code to ensure it runs during this course!

Leave a Comment