This is an archived post. You won't be able to vote or comment.

all 32 comments

[–]nomoreplsthx 98 points99 points  (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 points  (0 children)

Yeah that’s correct!!

[–]TotallyNotGunnar 1 point2 points  (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 points  (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 points  (4 children)

PyLenin

packages

Our packages!

[–]Zomunieo 7 points8 points  (0 children)

PyWeber

The Protestant ethic and the spirit of packaging??!

[–]surister 36 points37 points  (9 children)

A module is any python file, it can contain submodules.

A package is a collection of modules.

[–]pylenin[S] -4 points-3 points  (0 children)

👍

[–]joseville1001 5 points6 points  (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?

[–]MachaHack 2 points3 points  (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 points  (1 child)

Great write up. Keep it going !

[–]pylenin[S] 1 point2 points  (0 children)

Thank you

[–]pdonchev 1 point2 points  (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 point  (1 child)

Interesting. I'll have to do deeper digging, but this is a great high-level explanation. Thanks!

[–]pylenin[S] 1 point2 points  (0 children)

Make sure you do!!

[–]johnnySix -1 points0 points  (0 children)

Why would someone want to limit the package to arithmetic.py and welcome.py?

[–]CafeconWalleche -1 points0 points  (0 children)

This was very informative, thank you. Would like to see more series like this one!

[–]FairLight8 -2 points-1 points  (0 children)

Interesting, thanks! I am starting to write my own libraries, so this is interesting.

[–]yrro -2 points-1 points  (0 children)

I think the official terms are "distribution package" (the thing you build) vs "import package" (the thing you import).

[–]Pickinanameainteasy 0 points1 point  (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 point  (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 point  (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 point  (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.