Path and Environments

Core ideas:

    1. Directory structure navigation.
    1. Conda environment(s).
# You can run system commands from a Jupyter notebook with '!'

# Unix system
!pwd

# Windows system
!cd
To open a data file on the hard drive it’s ESSENTIAL to know its exact path.

Core ideas: 1. Relative paths. 2. Absolute paths. 3. Error Messages.

import numpy as np
rel_path = np.loadtxt("./data/EarthChemCU.txt", delimiter=',')
abs_path = np.loadtxt("/home/julian/Desktop/geopy/data/EarthChemCU.txt", delimiter=',')
# ERROR, read the error message
bad_path = np.loadtxt("../data/EarthChemCU.txt", delimiter=',')
## For windows: FICTIONAL paths to illustrate a point

rel_path = './Download/geo course/data/EarthChemCU.txt'
abs_path = 'C:/Users/jgio5642/Downloads/geo course/data/EarthChemCU.txt'
win_path = r'C:\Users\jgio5642\Download\geo course\data\EarthChemCU.txt'

# The 'r' makes a raw string - important because '\U' is a special python reserved character.
# programming method: to make paths useable
import glob
file = glob.glob('**/*.txt', recursive=True)
file
# the initial file we wanted
file[0]
# A full proof way of find files is to use "tab completion"

path = './

Conda environments

  • Conda is a “python” environment manager. Managing “complex” packages (python / R / C / Fortran, etc.) and dependencies.

  • Different styles of conda exist: anaconda, miniconda, mamba, etc, but the interface is the same. ( Difference is the default packages and internal dependency implementation ).

  • Conda can be available system-wide, or just per user.

    • In session 1 we downloaded our own i.e. per user.
    • I suspect some are using older or system installs.

  • Conda is designed to have many envrionments that can be switched in and out.

# Find what conda environments you have available
!conda info --env
# Find infomation about your conda installation - important if you change computers
!conda info
# Find what conda packages you have
!conda list

# Very similar to the pip list. The underlying python package manager
#!pip list
# python packages are `import'-ableVery similar to the pip list if you're familiar with pip
!conda env export --from-history

# This information could be copied into a file and used to download the same packages - this is how we created the initial environment in session 1.
# We use python "package" / "module" / "library" interchangably as users, when developing packages it's imporant to understand the difference but not for regular users.
# To download other packages
!conda search emoji

# if a package doesn't exist in the regular package channels, use '-c'
#!conda search -c conda-forge emoji
# To install a package by name, (required "-y")
!conda install emoji -y
import emoji
print(emoji.emojize("Python is fun :thumbs_up: :red_heart: :winking_face:"))
# To remove a package - take a LONG time
!conda remove emoji -y
!conda list | grep emoji

All materials copyright Sydney Informatics Hub, University of Sydney