Is Turtle a Module or Library in Python?

  • Context: Python 
  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    module Python
Click For Summary
SUMMARY

The discussion clarifies that in Python, Turtle is both a module and a library. It is classified as a module because it is distributed as a single file named turtle.py, while it is considered a library due to its common code usage across multiple applications. The terms "module," "library," and "package" are defined, with a module being a single file, a library being a collection of modules, and a package being a structured collection of modules. The import command in Python is used to load modules, which can be standalone or part of a package.

PREREQUISITES
  • Understanding of Python's module system
  • Familiarity with Python packages and libraries
  • Basic knowledge of Python's import statement
  • Awareness of Python's standard library
NEXT STEPS
  • Review Python's official documentation on the import system
  • Explore the structure and usage of Python packages
  • Investigate the Python standard library and its modules
  • Learn about the differences between modules, libraries, and packages in Python
USEFUL FOR

Python developers, educators, and learners seeking to deepen their understanding of Python's module and package architecture.

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   Reactions: jedishrfu and fog37
Thanks, as usual.
 

Similar threads

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