Python Is Turtle a Module or Library in Python?

  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    module Python
Click For Summary
The discussion clarifies the distinctions between modules, libraries, and packages in Python. A module is defined as a single file containing specific functionality, while a library is an informal term for a collection of reusable code, which can be a single module or a collection of modules. A package is a structured collection of modules that work together for a specific purpose. The command "import" is used to load modules into the interpreter, applicable to both standalone modules and those within packages. The Turtle graphics program is identified as both a module (as it is shipped as a single file) and a library (as it provides common functionality). Additionally, examples are provided to illustrate how to determine if a module is part of a package using Python's interactive session.
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:
Technology 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 jedishrfu and fog37
Thanks, as usual.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 25 ·
Replies
25
Views
6K
Replies
6
Views
3K
Replies
5
Views
15K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 5 ·
Replies
5
Views
1K