Is Turtle a Module or Library in Python?

  • Context: Python 
  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    module Python
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
fog37
Messages
1,566
Reaction score
108
TL;DR
module/library difference
Hello,
I am trying to fully understand the difference between module, library, and package in Python.

I think a module is a single file with a specific functionality.
A library is like a directory: it contains multiple files, i.e. multiple modules.
A package contains multiple libraries and sub-packages.

For example, is the graphic program Turtle a library or a module? The command import is used to import Turtle into the Python program. Is the command import used exclusively for modules?

Thanks!
 
Last edited:
Physics news on Phys.org
fog37 said:
I think a module is a single file with a specific functionality

That's the usual usage, yes.

fog37 said:
whereas a library contains multiple files, i.e. multiple modules. And a package contains multiple libraries?

No. A package is a collection of modules organized in a particular way, usually because all of the modules work together to provide a particular functionality.

The term "library" does not have a specific definition in the Python specifications; it's just used informally to describe common code that is used by multiple applications, and such code can be in a single module or it can be in a package.

fog37 said:
Is the command import used exclusively for modules?

The result of an import command is to load a particular module into the interpreter, but that module can be either a stand-alone module or a module that is part of a package.

A good reference is Python's import system documentation:

https://docs.python.org/3/reference/import.html
fog37 said:
is the graphic program Turtle a library or a module?

It's both. It's a module because that's how it's shipped, as a single turtle.py file. It's also a library because it is common code that can be used by multiple applications. It can also be considered part of the Python standard library, which is a collection of both modules and packages that provides a lot of standardized functionality for common use (as part of Python's "batteries included" philosophy).

If you want to do a quick test for a particular module to see whether it's part of a package, check out this interactive session at the Python interpreter:

Python:
Python 3.5.2 (default, Jul 10 2019, 11:58:48)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
>>> turtle.__name__
'turtle'
>>> turtle.__package__
''

By contrast, here is a module that is part of a package:

Python:
Python 3.5.2 (default, Jul 10 2019, 11:58:48)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from http import server
>>> server.__name__
'http.server'
>>> server.__package__
'http'
 
  • Like
Likes   Reactions: jedishrfu and fog37