Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Whenever you do "from 'x' import 'y'" I was wondering which one is considered the 'module' and which is the 'package', and why it isn't the other way around?

share|improve this question
1  
Regarding why it's the not the other way around, see stackoverflow.com/questions/3600352/… –  delnan Oct 1 '11 at 23:40
1  
The Python documentation provide formal, precise and complete definitions of both terms. docs.python.org/tutorial/modules.html#modules and docs.python.org/tutorial/modules.html#packages. What part of these was confusing? Please update your question to explain how the standard definitions in the documentation didn't help you. If you don't want to clarify your question, though, that's okay, too. –  S.Lott Oct 2 '11 at 0:28
add comment

1 Answer

up vote 45 down vote accepted

A Python module is simply a Python source file, which can expose classes, functions and global variables.

When imported from another Python source file, the file name is treated as a namespace.

A Python package is simply a directory of Python module(s).

For example, imagine the following directory tree in /usr/lib/python/site-packages:

mypackage/__init__.py <-- this is what tells Python to treat this directory as a package
mypackage/mymodule.py

So then you would do:

import mypackage.mymodule

or

from mypackage.mymodule import myclass
share|improve this answer
    
Aha, so when you say that the file name is treated as a namespace, you would so something like this? "mymodule.var1" for the first case, or "myclass.var1" for the second? –  Dark Templar Oct 2 '11 at 4:08
    
Not exactly. The package still counts as part of the namespace. If you want to do what you said then you need to do from mypackage import mymodule. –  Yam Marcovic Oct 2 '11 at 10:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.