import from a file in a subdirectory

Chris Liechti cliechti at gmx.net
Tue Apr 30 15:42:45 EDT 2002


rdacker at pacbell.net (rdack) wrote in 
news:644f6688.0204301127.5d3372d at posting.google.com:
> i have a script running in a directory.
> i want to call a subroutine in a file in a subdirectory.
> 
> in the first file i said  from subdir/ff.py import  subrtn
> python doesn't like the slash.
> 
> how do i do it?
 
this is simple:
>>> sys.path.append(os.path.abspath("subdir"))
>>> from ff import subrtn
the abspath is only needed when your programm changes the current
directory.


".py" is never needed when writing "import".

a package can be a subdirectory and then you can use
>>> import package.submodule
but then the "package" directory needs an "__init__.py"

HTH
chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list