Import module with non-standard file name

John Machin sjmachin at lexicon.net
Mon Aug 7 22:02:06 EDT 2006


Ben Finney wrote:
> Howdy all,
>
> Question: I have Python modules named without '.py' as the extension,
> and I'd like to be able to import them. How can I do that?
>
> Background:
>
> On Unix, I write programs intended to be run as commands to a file
> with no extension. This allows other programs to use the command as an
> interface, and I can re-write the program in some other language
> without obsoleting the commandline interface.
>
> e.g., I might write 'frobnicate-foo' as a shell program so that other
> programs can 'frobnicate-foo --bar baz'. If I later decide to
> re-implement 'frobnicate-foo' in Python, I'll save the top level
> module to the same file name since it implements the same command-line
> interface.
>
> Now that I've got it written as a Python module, I'd like to write
> unit tests for that module, which of course will need to import the
> program module to test it. The unit test can explicitly add the
> directory where the program module lives to 'sys.path' for the purpose
> of importing that module.

If it can do that, 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?

Cheers,
John




More information about the Python-list mailing list