Week 4: Thursday

Importing Packages

Libraries

What are Libraries/Modules?

  • A library is a collection of functions and methods that allows you to perform many actions without writing your code.
  • Python has a large standard library, commonly referred to as the Python Standard Library.
  • There are also many third-party libraries available that can be installed using package managers like pip.

Understanding Open Source

  • The code is freely available to copy, modify, and distribute.
  • The code is usually maintained by a community of developers.
  • Pandas source code

Installing Packages

  • Use pip to install packages.
  • pip install pandas
  • Do this from the command line or terminal.
  • if you have anaconda installed, you can use conda install pandas

Installing Packages, Google Colab

  • In Google Colab, you can install packages using !pip install pandas
  • Most of the popular packages are already installed in Google Colab.

Importing Packages

  • Use the import statement to import a package.
  • import pandas
  • The import is how we access the functions and classes in the package.

Example

# my_module.py
def my_function():
    print("Hello from my function")

def my_other_function():
    print("Hello from my other function")
# main.py
import my_module

my_module.my_function() 
# outputs "Hello from my function"

Using From to directly import specific functions

# my_module.py
def my_function():
    print("Hello from my function")

def my_other_function():
    print("Hello from my other function")

import using from

# main.py
from my_module import my_other_function

my_other_function()
# outputs "Hello from my other function"

Importing Packages, Aliasing

  • You can alias the package name to make it easier to use.
  • import pandas as pd
import pandas as pd

data = pd.DataFrame()