0

I was just trying to understand modules and packages in python, and as fas as I understood:

A module is a file containing Python definitions and statements.

A package is a directory containing modules or other packages.

Now, I made a very simple directory structure like below:

my_package/
 __init__.py
 my_module.py

Then, inside the interpreter, I did:

>>> import my_package
>>> type(my_package)
<type 'module'>
>>> from my_package import my_module
>>> type(my_module)
<type 'module'>

So, Python says, my_package and my_module, both are modules. Where is the package? Is package just a term used for description purposes and has no official identity as a class or object inside the core language? What are packages and modules for the interpreter?

asked Feb 1, 2014 at 16:47

1 Answer 1

1

Yes, a package can be imported as a module; the module's contents are in __init.py__.

The term package refers to the directory; it allows modules and subpackages to exist in the package namespace.

answered Feb 1, 2014 at 16:48
Sign up to request clarification or add additional context in comments.

2 Comments

So, it imports the package as a module. There is no term like package for a interpreter. Is it?
@ManishJain: the interpreter is aware of packages (e.g., it won't allow relative imports outside of them), but a package isn't a distinct object, no.

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.