Open in TheBrain

python MODULES

<p>Jan 27, 2013 04:25 PM

&#160;

&#62;&#62;&#62; import sys &#160;&#160;&#160;# imports al the system modules

&#160;

&#62;&#62;&#62; sys.path &#160;&#160;# &#160;shows the paths to the system modules as a list of paths.

note: &#160;all on the default path: &#160;C:\Python27

&#160;

['', 'C:\Python27\Lib\idlelib', 'C:\Python27\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages']

&#160;

&#62;&#62;&#62; <b>import mat</b>h &#160;# import is a reserved word &#160;(a command)e the math fx are.

0.5403023058681397

&#62;&#62;&#62; from math import cos

&#62;&#62;&#62; cos(1)

0.5403023058681397

&#160;

&#62;&#62;&#62;<b> from math import cos</b>

&#62;&#62;&#62; from math import sin

&#62;&#62;&#62; sin(1)2 + cos(1)2

0.9999999999999999

&#160;

Keep directory Python27 open and on monitor 4; &#160;when you run a module, you may see

a file like &#160;Hello.pyc &#160;&#160;(a compiled version of the file)... you can safely delete it.

&#160;

Note: <b>do not name modules with the same name as functions</b>...&#160;

it will cause ENDLESS CONFUSION;

&#160;

here's a script in the module Hello.py &#160;

&#160;

a='Sean'

&#160;

def Hellospec():

&#160;&#160;&#160;print 'Hello', a, ":now go away blablablabbity"

*

&#160;

to open the module you can do &#160;

import Hello... then, to invoke a function, you must do

&#160;

&#62;&#62;&#62; from Hello import Hellospec &#160;OR

you could invoke Hellospec &#160;by&#160;

&#160;

&#62;&#62;&#62;<b> Hello.Hellospec()</b> &#160;OR just do

&#160;

&#62;&#62;&#62;<b> from Hello import * </b> &#160;&#160;# there<b> * is a wildcard</b>, meaning &#160;import all the functions from Hello.

&#160;

&#62;&#62;&#62; # the way Sean works is to create a module for each project... module1.py

and to test it, then hit f5 from within module 1 &#160;(ie from the script window)

&#160;

&#62;&#62;&#62; # the other way is to work within the interpreter, and to import...

&#62;&#62;&#62; &#160;import Module1 &#160;&#160;...# no parens &#160;or

&#62;&#62;&#62; &#160;reload (Module1) &#160;... # reload does take parens...

&#160;

&#160;

&#160;

&#160;

&#160;

&#160;

&#160;</p>