CS 111 › Lesson 9 of 12

Modules, Packages & pip

Lesson 9 · CS 111: Programming I — Python · OKSTEM College

Modules, Packages & pip

Python's module system lets you split code across files and reuse libraries built by the community.

Work through the concepts using Python in your editor or Replit. Use the knowledge check below to test yourself.

Try It Yourself

Key Exercise

Open a Python file and implement the core concepts from this lesson. Start small, test each part, then combine.

Knowledge Check

How do you import only the sqrt function from the math module?

Wrong order - Python syntax is 'from module import name'.
Correct.
This doesn't work - use from math import sqrt.
That is JavaScript syntax.
Quick Recap

Python import syntax: from math import sqrt. Not 'import sqrt from math'.

Quick Recap

import math.sqrt is not valid Python. Either import math and use math.sqrt(), or from math import sqrt.

Quick Recap

Python uses import, not require. JavaScript uses require() or import.

What does pip install requests do?

pip installs packages; import loads them.
Correct.
venv does that, not pip install.
pip updates packages, not Python.
Quick Recap

pip is a command-line tool for installing packages from PyPI. import loads an already-installed package into your script.

Quick Recap

python -m venv creates a virtual environment. pip install downloads packages into it (or into your Python installation).

Quick Recap

pip manages Python packages. To update Python itself, download a new installer from python.org.

What is a virtual environment?

IDEs provide GUIs, not virtual environments.
Correct.
Cloud execution is different from virtual environments.
Virtual environments don't affect speed.
Quick Recap

A virtual environment is an isolated Python installation with its own packages, completely independent of others.

Quick Recap

Virtual environments run locally. They isolate package versions so Project A can use requests 2.0 while Project B uses requests 3.0.

Quick Recap

Virtual environments solve dependency isolation, not performance. Speed comes from algorithms and compiled extensions.

What does if __name__ == '__main__': do?

No performance effect.
Correct.
Python doesn't have a required main class.
main is not a built-in module.
Quick Recap

This guards code so it only runs when the file is executed directly, not when it's imported as a module.

Quick Recap

Python has no required main() or Main class. __name__ == '__main__' simply checks how the file is being run.

Quick Recap

This checks the __name__ variable. When run directly, __name__ is '__main__'. When imported, __name__ is the module name.

Which statement is true about Python modules?

You can install packages anywhere Python can find them.
Correct - any .py file is a module.
Third-party packages from PyPI are widely used.
Importing only runs top-level code, not function bodies.
Quick Recap

Python searches sys.path for modules: the script's folder, installed packages, and standard library locations.

Quick Recap

PyPI (Python Package Index) hosts hundreds of thousands of third-party packages installable with pip.

Quick Recap

Import executes top-level code (definitions, global assignments) but does NOT call functions. Functions only run when called.