Importing modules

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri May 1 02:50:57 CEST 2009


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.

-- 
Gabriel Genellina




More information about the Python-list mailing list