Importing modules

norseman norseman at hughes.net
Fri May 1 13:19:15 EDT 2009


Gabriel Genellina wrote:
> En Thu, 30 Apr 2009 14:33:38 -0300, Jim Carlock escribió:
> 
>> I'm messing around with a program right at the moment. It
>> ends up as two applications, one runs as a server and one
>> as a client which presents a Window. It almost works, so I
>> need to work through it to work out it's bugs, and I'll be
>> rewriting it in a couple other languages.
>>
>> Python looks fairly simple. It's a lot of "import" commands
>> and then "from" statements to do more importing. So I need
>> to figure out what the difference is between the two, as
>> the "from" seems to provide a way to identify what it wants
>> to import from the library/module.
> 
> Start by reading http://docs.python.org/tutorial
> In particular, section 6: Modules
> 
>> I find it odd that no one includes the FQFN and employs a
>> short name.
> 
> fully.qualified.module.name is long to type, error prone, and slow -- 
> Python has to resolve each dotted name at runtime, every time it's used.
> 
> So writting this is common:
> from fully.qualified.module import name
> and then, just use `name` in the code. By looking at the `import` lines, 
> usually located at the top, you know where a certain name comes from.
> 
> ...unless there are statements like this:
> from somemodule import *
> which are considered bad practice anyway.
> 
>> Nothing seems to get SET in the Environment to
>> identify where the library gets configured. I have to run
>> off to find help on the differences between "import" and
>> "from".
> 
> No need for that, usually. There is a default library search path that 
> is built relative to the interpreter location. That is, if the Python 
> interpreter used is /usr/some/fancy/directory/python, then the standard 
> library is at /usr/some/fancy/directory/lib/python2.6, additional 
> packages are at /usr/some/fancy/directory/lib/python2.6/site-packages, etc.
> 
> You *can* alter the search path by setting some environment variables, 
> but I don't like that.
> 
------------------------------
I would like to make a small comment here on Python locations.
Windows seems to have to have a slight difference.

...\python\Lib                   #py libs go here
...\pythonVer\Lib\site-packages  #addons usually go here same as Linux
...\pythonVer\libs             #seems to hold MicroSoft specific py libs


Like Gabriel - my Linux is same as he describes.
                except I don't have a /usr/...ory/python
                in my case it's all in   /usr/local/lib/python2.5
                  to keep it all in one place
                  /usr/local/bin   has the soft links to run it
                  (reduces PATH size.  /usr/local/bin  already there)

Which shows how flexible Python is.


OH - something you mentioned that didn't seem to be addressed.
import - load a complete library
from   - obtain specific 'function'(s) from a library

(from mylib import fopen, fclose     or  from mylib import f*
                    one or more           gets fopen, fclose, etc

  from mylib import fopen as myfopen   can be useful  myfopen(...)
                                       won't be confused with the system
                                       fopen(...)
  which is great for not having to  MyVeryLongNameLib.fopen()  :)
)

The cautionary note here is, IF two libs have a function with the same 
name and 'from' is used to get it from each, you wind up the last one
requested.
To alleviate the problem you can  open(...)  or mylib.open(...) and thus 
access both (or even more) in the same program.  Check the built-ins and 
the libs for name problems and use the techniques noted to get what you 
want.

Not sure what is in a given library?
import somelib
help(somelib)
   (and read)

When it come to showing off, Python has its moments!

HTH

Steve



More information about the Python-list mailing list