Can I import from a directory that starts with a dot (.) ?

Jason Scheirer jason.scheirer at gmail.com
Tue Apr 14 02:11:31 EDT 2009


On Apr 13, 5:51 pm, Matthew Wilson <m... at tplus1.com> wrote:
> I want to have .foo directory that contains some python code.  I can't
> figure out how to import code from that .foo directory.  Is this even
> possible?
>
> TIA
>
> Matt

Yes, but it's not a particularly recommended thing to do as it is
CRAZY. You can get to it from the imp module's functions:

import imp
# Loads a file/dir named .foo or .foo.py on the sys.path, giving it
the overloaded name of foo in sys.modules
imp.load_module('foo', *imp.find_module('.foo'))
# Grab the module into the local namespace, it's been imported in the
system but not here yet
import foo

Seriously. Crazy. It looks strange and hard because it is in no way a
use case that anyone expects you to have. But it's possible.



More information about the Python-list mailing list