Import module with non-standard file name

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Aug 7 23:21:36 EDT 2006


"John Machin" <sjmachin at lexicon.net> writes:

> Ben Finney wrote:
> > "John Machin" <sjmachin at lexicon.net> writes:
> > > If it can [modify sys.path], it can copy the MUT to some temp
> > > directory, adding .py to the end of the name of the new file,
> > > and put the temp directory in sys.path .... can't it?
> >
> > The problem with importing the program module from a file in a
> > different directory is that the program won't be able to find its
> > own relative modules. That leads to either *more* sys.path
> > hackery, or importing from a temporary file in the *same*
> > directory.
> 
> Please explain both the "own" and "relative" in "its own relative
> modules". Do these modules not have names that end in ".py"?

The program can import modules with relative paths, because it can
expect its position in the directory tree to remain the same relative
to those modules. If the program module suddenly exists in a different
directory, that assumption no longer holds and the relative imports
performed by the program will fail.

Thus to avoid that problem, testing needs to be done on the program
module with its position relative to all other modules the same as
when that program runs.

-- 
 \       "It may be that our role on this planet is not to worship God |
  `\                       -- but to create him."  -- Arthur C. Clarke |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list