compie compie - 3 months ago 34x
Python Question

Import a python module without the .py extension

I have a file called foobar (without .py extension). In the same directory I have another python file that tries to import it:

import foobar

But this only works if I rename the file to Is it possible to import a python module that doesn't have the .py extension?

Update: the file has no extension because I also use it as a standalone script, and I don't want to type the .py extension to run it.

Update2: I will go for the symlink solution mentioned below.


You can use the imp.load_source function (from the imp module), to load a module dynamically from a given file-system path.

foobar = imp.load_source('foobar', '/path/to/foobar')

This SO discussion also shows some interesting options.