klenwell information services : PywellModules

Revision history for PywellModules


Revision [3050]

Last edited on 2016-07-09 16:54:44 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
return to [[PyWell | Tutorial Index]]
Modules define namespaces, which keep your code neatly organized and help avoid [[http://en.wikipedia.org/wiki/Naming_collision | naming collisions]].
Python comes with many builtin modules, such as the [[http://docs.python.org/2/library/sys.html | sys module]] to which you've already been introduced. Other builtin modules include ##os##, ##math##, and ##re## for [[http://en.wikipedia.org/wiki/Regular_expressions | regular expressions]]. Because of the breadth and utility of its builtin modules, Python is often touted as coming "batteries included," a feature that has been famously satirized here:
~- [[http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH | Python Documentation on PYTHONPATH]]
~- [[http://docs.python.org/2/tutorial/modules.html | Python Documentation on Modules]]
~- [[http://www.doughellmann.com/PyMOTW/ | PyMOTW: Python Module of the Week]]
Deletions:
return to [[PyWell Tutorial Index]]
Modules define namespaces, which keep your code neatly organized and help avoid [[http://en.wikipedia.org/wiki/Naming_collision naming collisions]].
Python comes with many builtin modules, such as the [[http://docs.python.org/2/library/sys.html sys module]] to which you've already been introduced. Other builtin modules include ##os##, ##math##, and ##re## for [[http://en.wikipedia.org/wiki/Regular_expressions regular expressions]]. Because of the breadth and utility of its builtin modules, Python is often touted as coming "batteries included," a feature that has been famously satirized here:
~- [[http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH Python Documentation on PYTHONPATH]]
~- [[http://docs.python.org/2/tutorial/modules.html Python Documentation on Modules]]
~- [[http://www.doughellmann.com/PyMOTW/ PyMOTW: Python Module of the Week]]


Revision [2376]

Edited on 2013-01-01 22:14:44 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
As you venture into Python, you'll see this mysterious ""<tt>__init__.py</tt>"" file a lot. You can even include code in it. But it's primary function is to say to the interpreter, "Hey! This directory is a Python module."
Deletions:
As you venture into Python, you'll see this mysterious ##__init__.py## file a lot. You can even include code in it. But it's primary function is to say to the interpreter, "Hey! This directory is a Python module."


Revision [2366]

Edited on 2012-12-24 12:59:26 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~1) What is happening in the ""<tt>__name__ == "__main__"</tt>"" block of the ##stupid## module above?
Deletions:
~1) What is happening in the ##__name__ == "__main__"## block of the ##stupid## module above?


Revision [2365]

Edited on 2012-12-24 12:58:41 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~1) What is happening in the ##__name__ == "__main__"## block of the ##stupid## module above?
~1) Either write a script or from the command line import your flying module and call the fly function. (Hint: if you're working in the interactive interpreter, the ##sys## module's ##path## attribute may be of some help.)
Deletions:
Either write a script or from the command line import your flying module and call the fly function. (Hint: if you're working in the interactive interpreter, the ##sys## module's ##path## attribute may be of some help.)


Revision [2364]

Edited on 2012-12-24 12:57:30 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
Either write a script or from the command line import your flying module and call the fly function. (Hint: if you're working in the interactive interpreter, the ##sys## module's ##path## attribute may be of some help.)
~- [[http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH Python Documentation on PYTHONPATH]]
~- [[http://www.doughellmann.com/PyMOTW/ PyMOTW: Python Module of the Week]]
Deletions:
Either write a script or from the command line import your ##flying## module and call the ##fly## function. (Hint: if you're working in the interactive interpreter, the ##sys## module's ##path## attribute may be of some help.)
~- [[http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH PYTHONPATH]]


Revision [2362]

Edited on 2012-12-24 12:52:58 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
Either write a script or from the command line import your ##flying## module and call the ##fly## function. (Hint: if you're working in the interactive interpreter, the ##sys## module's ##path## attribute may be of some help.)
Deletions:
Either write a script or from the command line import your flying module and call the fly function. (Hint: if you're working in the interactive interpreter, the ##sys## module's ##path## attribute may be of some help.)


Revision [2360]

Edited on 2012-12-24 12:49:35 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
Python comes with many builtin modules, such as the [[http://docs.python.org/2/library/sys.html sys module]] to which you've already been introduced. Other builtin modules include ##os##, ##math##, and ##re## for [[http://en.wikipedia.org/wiki/Regular_expressions regular expressions]]. Because of the breadth and utility of its builtin modules, Python is often touted as coming "batteries included," a feature that has been famously satirized here:
~- [[http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH PYTHONPATH]]
Deletions:
Python comes with many builtin modules, such as the [[http://docs.python.org/2/library/sys.html sys module]] to which you've already been introduced. Other builtin modules include ##os##, ##math##, and ##re## for [[ regular expressions]]. Because of the breadth and utility of its builtin modules, Python is often touted as coming "batteries included," a feature that has been famously satirized here:
~- PYTHONPATH


Revision [2359]

Edited on 2012-12-24 12:45:56 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~- Introduce namespace concept
~- Explore Python's native modules (or "batteries")
If you were developing a more complex library, you'd probably want your module to have multiple files organized into a directory. To do that, create a directory with your module name and add your python files to that. Then to inform the interpreter that you want this directory to be treated as a module that can be imported, simply add an empty file named ##__init__.py##. The resulting directory tree will look something like this:
===Namespaces===
===Batteries Included===
Python comes with many builtin modules, such as the [[http://docs.python.org/2/library/sys.html sys module]] to which you've already been introduced. Other builtin modules include ##os##, ##math##, and ##re## for [[ regular expressions]]. Because of the breadth and utility of its builtin modules, Python is often touted as coming "batteries included," a feature that has been famously satirized here:
http://xkcd.com/353/
Create a module named ##antigravity## that includes a submodule named ##flying## which includes the following function:
def fly():
print "Oh, if only it were this easy."
Either write a script or from the command line import your flying module and call the fly function. (Hint: if you're working in the interactive interpreter, the ##sys## module's ##path## attribute may be of some help.)
Deletions:
~- Introduce Python's native modules (or "batteries")
If you were developing a more complex library, you'd probably want your module to have multiple files organized into a directory. To do that, create a directory with your module name and add your python files to that. Then to inform the interpreter that you want this directory to be treated as a module that can be imported, simply add an empty file named ""<tt>__init__.py</tt>"". The resulting directory tree will look something like this:


Revision [2358]

Edited on 2012-12-24 12:31:37 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
Assuming the file was named ##stupid.py## and on your ##PYTHONPATH##*, you could use it from the interpreter like so:
If you were developing a more complex library, you'd probably want your module to have multiple files organized into a directory. To do that, create a directory with your module name and add your python files to that. Then to inform the interpreter that you want this directory to be treated as a module that can be imported, simply add an empty file named ""<tt>__init__.py</tt>"". The resulting directory tree will look something like this:
|-- simple.py
|-- hard.py
\-- __init__.py
//Namespaces are one honking great idea -- let's do more of those!//
Deletions:
Assuming the file was named ##stupid.py# and on your ##PYTHONPATH##*, you could use it from the interpreter like so:
If you were developing a more complex library, you'd probably want your module to have multiple files organized into a directory. To do that, create a directory with your module name and add your python files to that. Then to inform the interpreter that you want this directory to be treated as a module that can be imported, simply add an empty file named ##__init__.py##. The resulting directory tree will look something like this:
├── simple.py
├── hard.py
└── __init__.py
**Namespaces are one honking great idea -- let's do more of those!**


Revision [2357]

Edited on 2012-12-24 12:30:00 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
stupid.py
A stupid simple Python module
print "simple (but not very useful)"
If you were developing a more complex library, you'd probably want your module to have multiple files organized into a directory. To do that, create a directory with your module name and add your python files to that. Then to inform the interpreter that you want this directory to be treated as a module that can be imported, simply add an empty file named ##__init__.py##. The resulting directory tree will look something like this:
stupid
├── simple.py
├── hard.py
└── __init__.py
The ##simple## module could then be imported by a Python script in a couple different ways:
# as part of the stupid namespace
import stupid
stupid.simple.function_that_does_something()
# or directly using from... import syntax
from stupid import simple
simple.function_that_does_something()
The beauty of modules is that they provide a rather elegant and intuitive way to organize your code. You may remember the final line from the Zen of Python:
**Namespaces are one honking great idea -- let's do more of those!**
Modules define namespaces, which keep your code neatly organized and help avoid [[http://en.wikipedia.org/wiki/Naming_collision naming collisions]].
~- [[http://docs.python.org/2/tutorial/modules.html Python Documentation on Modules]]
Deletions:

print "simple (but not very useful")
If you were developing a more complex library, you'd probably want your module to have multiple files organized into a directory. To do that, create a directory with your module name and add your python files to that. Then to inform the interpreter that you want this directory to be treated as a module that can be imported, simply add an empty file named ##__init__.py##.


Revision [2356]

The oldest known version of this page was created on 2012-12-24 12:15:22 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]