pip, the default Python package managerIn the previous module, we learn how to write functions, our lowest level modules (black box).
Modular programming refers to the process of breaking a large, unwieldy programming task into separate, smaller, more manageable units or modules (like functions!)
Individual modules can then be cobbled together like building blocks to create new or larger applications.
As your program gets bigger, you may want to split it into several files for easier maintenance and reusability.
Python has a way to put definitions in a file and use them in a script: such a file is called a module.
Definitions (variables, functions) from a module can be imported into other modules or into the main module (entry point of your program)
We already have used built-in modules:
math,random,datetime, etc. !
Functions, modules and packages are all constructs in Python that promote code modularization!
A Python module is just a file containing Python definitions and statements.
The file name is the module name
with the suffix .py appended.
Create a file that contains Python code and then
give the file a name with a .py extension. That’s it!
For example, suppose you have created a file called
my_module.py containing the following:
s = "An now, for something completely different"
a = [1, 1, 2]
def foo():
print("function foo called")Create another file, app.py (your main program file) and
import your module (they should be both in the same directory!). To make
the module available to your program, use the
import my_module statement.
To access module content (functions, variables, classes,
etc.), you must prefix each call with the module name
using the dot (.) operator:
import my_module
# Access module content (variables, functions) with the dot operator
print(my_module.s)
print(my_module.a)
my_module.foo()When the interpreter executes the above
importstatement, it searches formy_module.pyin a list of directories including the current directory.
# Import my_module with an alias
import my_module as mm
# s is accessed under the 'mm' namespace, hence we need the dot operator
print(mm.s)from my_module import foo, s
print(s) # Notice that we don't need to prefix s and foo here
print(foo())Importing functions directly into the global namespace can
silently overwrite existing variables or functions with the
same name. If you already have a foo() function
into your main program (global namespace) it will overwrite and hide the
imported one
# Imports foo from my_module directly into the global namespace under the name 'foo'.
from my_module import foo
# Redefines the identifier foo in the same "global scope". The original imported reference is overwritten and lost.
def foo():
print("foo main program")
# Executes the local foo() function and prints "foo main program"
foo()The last definition in the same scope always wins. Use aliases or namespace imports to keep scope clean.
The import statement can take many different forms,
shown below:
# Import a module
import <module_name>
# Multiple imports on the same line
import <module_name>[, <module_name> ...]
# Import only a few objects (functions) given by the module
from <module_name> import <name(s)>
# Import only a few objects (functions) given by the module under an alternate name (aliases)
from <module_name> import <name> as <alt_name>
# Import the entire module under an alternate name
import <module_name> as <alt_name># Basic import of a single module
import math
# Usage: Access contents using the module name prefix
print(math.sqrt(16)) # Outputs: 4.0
# Multiple imports on the same line
import sys, os
# Usage: Access both modules independently
print("sys version:", sys.version)
print("os name: ", os.name)
# Import specific objects (functions/classes) directly
from datetime import datetime, date
# Usage: Use imported objects directly without module prefix
now = datetime.now()
today = date.today()
print(now, today)
# Import specific objects with alternate names (aliases)
from random import randint as ri
# Usage: Use the function via its alias 'ri' (which is a terrible name by the way!)
dice = ri(1, 6)
print(f"You rolled {dice}!")A package is a coherent collection of modules. It’s a folder containing multiple modules and sub-packages (recursive structure).
You can create your own packages (outside of the scope of this course) or install useful third-party packages provided by open-source community or external vendors. These include essential libraries for:
PyCharm makes it easy to install, uninstall and upgrade Python packages.
By default, PyCharm uses pip to manage project packages.
In PyCharm, you can preview and manage packages in the Python Packages tool window.
Each project has its own set of packages.
Create a new project
(File > New Project...) called
DataAnalysis
For the rest of this course, we are going to work within this project!
pip (the package manager) for nownumpymatplotlibpandastest.py# Test to check everything is OK
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# NumPy: Create numerical data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Pandas: Store and manipulate data in a DataFrame
df = pd.DataFrame({"X": x, "Sin_X": y})
print("First 5 rows of data:")
print(df.head())
# Matplotlib: Plot the data
plt.plot(df["X"], df["Sin_X"], label="Sin(x)")
plt.title("If you see the sin curve, everything is OK !")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
# Display the plot
plt.show()