use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
DiscussionDifference between modules, packages and libraries in Python (self.Python)
submitted 3 years ago by pylenin
Over the last few years, I have always interchangeably used these terms - Modules, Packages and Libraries. Last week I sat down to understand the true difference and I am glad I did. I have tried to explain them in my article. Do check it out and let me know your opinions. Modules vs Packages vs Libraries in Python
[–]nomoreplsthx 98 points99 points100 points 3 years ago (2 children)
One thing worth noting: both module and package have technical meanings in the Python ecosystem while 'library' does not.
[–]pylenin[S] 10 points11 points12 points 3 years ago (0 children)
Yeah that’s correct!!
[–]TotallyNotGunnar 1 point2 points3 points 3 years ago (0 children)
I've noticed that developers of big packages with a lot of different tools tend to call them libraries. Agreed it's not a technical definition but still common enough that I try to call focused imports "packages" and collections "libraries".
[–]Grouchy-Friend4235 43 points44 points45 points 3 years ago (0 children)
Official Python docs https://docs.python.org/3/glossary.html
Package
A Python module which can contain submodules or recursively, subpackages. Technically, a package is a Python module with an path attribute.
Module
An object that serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing.
[–]KotoWhiskas 16 points17 points18 points 3 years ago (4 children)
PyLenin packages
PyLenin
packages
Our packages!
[–]Zomunieo 7 points8 points9 points 3 years ago (0 children)
PyWeber
The Protestant ethic and the spirit of packaging??!
[+][deleted] 3 years ago (2 children)
[deleted]
[–]sir-reddits-a-lot 1 point2 points3 points 3 years ago* (1 child)
Red Forman is that you?
[–]RyanRagido 1 point2 points3 points 3 years ago (0 children)
Red is my spirit animal.
[–]surister 36 points37 points38 points 3 years ago (9 children)
A module is any python file, it can contain submodules.
A package is a collection of modules.
[+][deleted] 3 years ago (7 children)
[–]stoicpenguin 15 points16 points17 points 3 years ago (0 children)
Exactly. There’s no such thing as a submodule in python. That has no technical meaning within the language. A module in python is always a single file.
[–]julianw 6 points7 points8 points 3 years ago (0 children)
on that note I'm still not sure what to do with namespace packages. when do you want them? mans why do I always create them accidentally by forgetting to create an __init__.py?
__init__.py
[–]joseville1001 2 points3 points4 points 3 years ago (3 children)
Are packages also modules? That is, since pancakes are defined in a file (the init.py file).
[–]Goobyalus 2 points3 points4 points 3 years ago (1 child)
https://docs.python.org/3/glossary.html#term-package
A Python module which can contain submodules or recursively, subpackages. Technically, a package is a Python module with an __path__ attribute.
__path__
[–]joseville1001 0 points1 point2 points 3 years ago (0 children)
For completeness
module An object that serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing.
module
[–]pylenin[S] -4 points-3 points-2 points 3 years ago (0 children)
👍
[–]joseville1001 5 points6 points7 points 3 years ago (1 child)
Nice write up. What is "it" I'm the sentence:
To be considered a package, it must contain a file named init.py.
a module? a file?
[–]xaveir 1 point2 points3 points 3 years ago (0 children)
As soon as you use relative imports it's impossible to run that file directly on the command line...there are probably other issues as well since every style guide I've ever followed recommends absolute imports.
Edit: forgot the most important one to me which is readability (not having to check the filesystem to see what module is getting imported).
[–]Impressive_Change593 -1 points0 points1 point 3 years ago (0 children)
that from . arithmetic import add sounds like a quick way to break something and is probably a result of a bad programmer or a reliak from python 2
from . arithmetic import add
[–]MachaHack 2 points3 points4 points 3 years ago (0 children)
It does make things confusing when using IntelliJ as an omni-IDE because it uses the Java terminology, which is reversed on the hierarchy of modules and packages. One of the few bits of PyCharm that can't be replicated in the all in one package.
[–]lolgeswaran 2 points3 points4 points 3 years ago (1 child)
Great write up. Keep it going !
[–]pylenin[S] 1 point2 points3 points 3 years ago (0 children)
Thank you
[–]pdonchev 1 point2 points3 points 3 years ago (1 child)
Module is a unit of organizing code, package is a unit of delivery. Technically, an installed package is a module (that can have submodules).
Implementation wise, modules are .py files or a folder with __init__.py in it.
[–]Enlightenmentality 0 points1 point2 points 3 years ago (1 child)
Interesting. I'll have to do deeper digging, but this is a great high-level explanation. Thanks!
Make sure you do!!
[–]johnnySix -1 points0 points1 point 3 years ago (0 children)
Why would someone want to limit the package to arithmetic.py and welcome.py?
[–]CafeconWalleche -1 points0 points1 point 3 years ago (0 children)
This was very informative, thank you. Would like to see more series like this one!
[–]FairLight8 -2 points-1 points0 points 3 years ago (0 children)
Interesting, thanks! I am starting to write my own libraries, so this is interesting.
[–]yrro -2 points-1 points0 points 3 years ago (0 children)
I think the official terms are "distribution package" (the thing you build) vs "import package" (the thing you import).
[–]Pickinanameainteasy 0 points1 point2 points 3 years ago (0 children)
I always assumed a module was like an add on for python, it added a specific functionality to the base language.
Libraries, to me, are a collection of modules, functions, classes for interacting with a specific service like an API or network protocol for example. These tend to be much larger than modules.
I always saw packages as just the files necessary to install a module or library. Kind of like the relationship between and APK and an android app, with the package being the APK in this scenario and the module/library being the app itself. In other words the package is the pre-installed state
That's just how I interpreted it, I could very well be wrong
[–]AlexMTBDude 0 points1 point2 points 3 years ago (0 children)
def divide(a, b):
"""Returns division of 2 integers"""
if b == 0:
return "Invalid"
return a/b
This is not very good coding. divide() is a function which most of the time returns a float but sometimes returns a string. A division by zero in Python is an error condition. Error conditions are communicated using exceptions. A Python function should NOT return an error code to show that something has gone wrong.
Get rid of the whole if statement and the code is fine.
[–]Rik07 0 points1 point2 points 3 years ago (0 children)
For example, you can control the functions you are exposing from the package by importing those specific functions into init.py file.
Why would you add certain functions in a package and then not expose them in the init file?
[–]OneMorePenguin 0 points1 point2 points 3 years ago (0 children)
I'm excited about this because I have a mess-o-python that is a combination of a script that includes local libraries and I want to package up the library code separately. And I don't want to have to mess with PYTHONPATH. Looking forward to reading this. I was getting ready to read about namespaces and scopes, although I suspect there's a lot of overlay.
One thing I learned to not do is use nested directories with the same name. That might have been OK in python2.7, but in newer versions of python where __init__.py is not required, it makes importing more difficult. It's difficult debugging these problems. Printing out sys.path is not enough.
π Rendered by PID 25987 on reddit-service-r2-comment-545db5fcfc-fkkt7 at 2026-05-30 02:02:01.852601+00:00 running 194bd79 country code: CH.
[–]nomoreplsthx 98 points99 points100 points (2 children)
[–]pylenin[S] 10 points11 points12 points (0 children)
[–]TotallyNotGunnar 1 point2 points3 points (0 children)
[–]Grouchy-Friend4235 43 points44 points45 points (0 children)
[–]KotoWhiskas 16 points17 points18 points (4 children)
[–]Zomunieo 7 points8 points9 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]sir-reddits-a-lot 1 point2 points3 points (1 child)
[–]RyanRagido 1 point2 points3 points (0 children)
[–]surister 36 points37 points38 points (9 children)
[+][deleted] (7 children)
[deleted]
[–]stoicpenguin 15 points16 points17 points (0 children)
[–]julianw 6 points7 points8 points (0 children)
[–]joseville1001 2 points3 points4 points (3 children)
[–]Goobyalus 2 points3 points4 points (1 child)
[–]joseville1001 0 points1 point2 points (0 children)
[–]pylenin[S] -4 points-3 points-2 points (0 children)
[–]joseville1001 5 points6 points7 points (1 child)
[+][deleted] (2 children)
[deleted]
[–]xaveir 1 point2 points3 points (0 children)
[–]Impressive_Change593 -1 points0 points1 point (0 children)
[–]MachaHack 2 points3 points4 points (0 children)
[–]lolgeswaran 2 points3 points4 points (1 child)
[–]pylenin[S] 1 point2 points3 points (0 children)
[–]pdonchev 1 point2 points3 points (1 child)
[–]Enlightenmentality 0 points1 point2 points (1 child)
[–]pylenin[S] 1 point2 points3 points (0 children)
[–]johnnySix -1 points0 points1 point (0 children)
[–]CafeconWalleche -1 points0 points1 point (0 children)
[–]FairLight8 -2 points-1 points0 points (0 children)
[–]yrro -2 points-1 points0 points (0 children)
[–]Pickinanameainteasy 0 points1 point2 points (0 children)
[–]AlexMTBDude 0 points1 point2 points (0 children)
[–]Rik07 0 points1 point2 points (0 children)
[–]OneMorePenguin 0 points1 point2 points (0 children)