order of importing modules

Chris Rebert crebert at ucsd.edu
Wed Jan 12 16:31:59 EST 2011


> Dan Stromberg wrote:
>> On Tue, Jan 11, 2011 at 4:30 PM, Catherine Moroney
>> <Catherine.M.Moroney at jpl.nasa.gov> wrote:
>>>
>>> In what order does python import modules on a Linux system?  I have a
>>> package that is both installed in /usr/lib64/python2.5/site-packages,
>>> and a newer version of the same module in a working directory.
>>>
>>> I want to import the version from the working directory, but when I
>>> print module.__file__ in the interpreter after importing the module,
>>> I get the version that's in site-packages.
>>>
>>> I've played with the PYTHONPATH environmental variable by setting it
>>> to just the path of the working directory, but when I import the module
>>> I still pick up the version in site-packages.
>>>
>>> /usr/lib64 is in my PATH variable, but doesn't appear anywhere else.  I
>>> don't want to remove /usr/lib64 from my PATH because that will break
>>> a lot of stuff.
>>>
>>> Can I force python to import from my PYTHONPATH first, before looking
>>> in the system directory?
>>>
>> Please import sys and inspect sys.path; this defines the search path
>> for imports.
>>
>> By looking at sys.path, you can see where in the search order your
>> $PYTHONPATH is going.
>>
On Wed, Jan 12, 2011 at 11:07 AM, Catherine Moroney
<Catherine.M.Moroney at jpl.nasa.gov> wrote:
> I've looked at my sys.path variable and I see that it has
> a whole bunch of site-package directories, followed by the
> contents of my $PYTHONPATH variable, followed by a list of
> misc site-package variables (see below).
<snip>
> But, I'm curious as to where the first bunch of 'site-package'
> entries come from.  The
> /usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg
> is not present in any of my environmental variables yet it shows up
> as one of the first entries in sys.path.

You probably have a .pth file somewhere that adds it (since it's an
egg, probably site-packages/easy-install.pth).
See http://docs.python.org/install/index.html#modifying-python-s-search-path

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list