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.
Open a Python file and implement the core concepts from this lesson. Start small, test each part, then combine.
How do you import only the sqrt function from the math module?
Python import syntax: from math import sqrt. Not 'import sqrt from math'.
import math.sqrt is not valid Python. Either import math and use math.sqrt(), or from math import sqrt.
Python uses import, not require. JavaScript uses require() or import.
What does pip install requests do?
pip is a command-line tool for installing packages from PyPI. import loads an already-installed package into your script.
python -m venv creates a virtual environment. pip install downloads packages into it (or into your Python installation).
pip manages Python packages. To update Python itself, download a new installer from python.org.
What is a virtual environment?
A virtual environment is an isolated Python installation with its own packages, completely independent of others.
Virtual environments run locally. They isolate package versions so Project A can use requests 2.0 while Project B uses requests 3.0.
Virtual environments solve dependency isolation, not performance. Speed comes from algorithms and compiled extensions.
What does if __name__ == '__main__': do?
This guards code so it only runs when the file is executed directly, not when it's imported as a module.
Python has no required main() or Main class. __name__ == '__main__' simply checks how the file is being run.
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?
Python searches sys.path for modules: the script's folder, installed packages, and standard library locations.
PyPI (Python Package Index) hosts hundreds of thousands of third-party packages installable with pip.
Import executes top-level code (definitions, global assignments) but does NOT call functions. Functions only run when called.