1

I was reading a document and I encountered the part below:

A ​module​ is nothing more, nothingless than a directory with ​__init__.py​ file inside. It should contain other Python files with the logic related to the module's purpose.

Is this definition correct?

(As I know, a Python module is a file, whose extension is py, which contains Python code. And as I remember a directory which contains dunder init(__init__.py) file is called as Python package.)

Azat Ibrakov
11.1k9 gold badges43 silver badges58 bronze badges
asked Apr 7, 2021 at 19:58
3
  • 3
    A package is also a module, in that I can import it by name. I would call the definition just a bit sloppy, since it doesn't allow for single-file modules. Commented Apr 7, 2021 at 20:02
  • 3
    Python docs may help Commented Apr 7, 2021 at 20:02
  • 1
    Both definitions are bad. Modules don't need to be .py files (they can be .so for example), and packages don't need to be directories on disk (they can be zipfiles for example). Modules and packages may be dynamically created in memory and may not exist on a filesystem at all. And namespace packages are a type of Python package which does not have any __init__.py. Commented Apr 7, 2021 at 20:17

1 Answer 1

1

The definition is incorrect.

While a "package" is also a "module", a single ".py" file comprises, in itself, a module, with no need to be a directory, or have an __init__ file inside it.

A package, on the other hand, yes, is usually (and defined as) a directory with an __init__.py and optionally other files.

(However packages can be presented in other formats such as .egg or zip files, or even custom formats with corresponding customized importing mechanisms, which are plugable)

answered Apr 7, 2021 at 20:19
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.