How Python Modules & Packages work. Application: Energy
Packages & Modules in Energy
If you’ve been using Python for a while, you’ve probably written import something hundreds of times. Have you ever stopped to think about what exactly you’re importing? Is it a module? A package? Does it matter? Yes, it does matter. And understanding the difference will make you a better Python developer.
Let’s start with the fundamentals.
What is a Module?
A module is simply any .py file. That’s it.
When you create a file called wind.py or solar.py or main.py, you’ve created a module. It doesn’t matter if the file is empty or contains thousands of lines of code. If it ends in .py, it’s a module.
Think of a module as a single book. It contains code (functions, classes, variables) that you can import and use elsewhere. Examples of modules you probably use all the time:
math- Python’s built-in math modulerandom- for generating random numbers
What is a Package?
A package is a folder that contains a special file called __init__.py along with one or more modules.
The __init__.py file is what transforms an ordinary directory into a package. This file can be empty (and often is for beginners), but its presence tells Python: “Treat this folder as a package that can be imported.”
Think of a package as a library building that contains multiple books (modules) organized together. Examples of popular packages:
pandas- for data analysisnumpy- for numerical computingpvlib- for solar photovoltaic system modeling (we’ll use this later)
What is a Directory?
A directory is simply a folder without the __init__.py file. You can import directories. They become what are called “namespace packages” or “implicit namespace packages.” So import directory_energy works just fine.
The difference between a package and a directory is the presence of __init__.py. Add the file, and you get a Python package. Remove it, and you get a namespace package (a directory) that’s just a container.
Installing Packages in PyCharm
Quick tip for PyCharm users on macOS, to install a package like pvlib:
Go to PyCharm menu → Settings
Navigate to Python → Interpreter
Click the + button
Search for the package (e.g.,
pvlib)Click Install Package
Creating Packages and Modules in PyCharm
In PyCharm, when you click the + button in the Project view:
Python Package : Creates a folder with
__init__.pyPython File : Creates a module (
.pyfile)Directory : Creates a plain folder (no
__init__.py)
What you can do with __init__.py
Full code below with the Python model.
Join our community! Visit skool.com, search for “Energy Data Scientist”


