How can I import a py script by its absolute path name?

Chris Lambacher lambacck at computer.org
Thu Jul 14 09:27:50 EDT 2005


You probably actually want:

import sys
sys.path.instert(0, r'c:\xxx\yyy')
m = __import__('zzz', globals(),  locals(), [])
del sys.path[0]

Because if another module named zzz exists in your path.  Appending will pick
those versions up first.  Then you delete the path you just added so that you
don't have any problems importing other modules that may have the same names a
python files in the path you just added.

-Chris
On Thu, Jul 14, 2005 at 02:52:31PM +0300, Edvard Majakari wrote:
> could ildg <could.net at gmail.com> writes:
> 
> > I want to import c:\xxx\yyy\zzz.py into my programme,
> > What should I do?
> > Thank you~
> 
> import sys
> sys.path.append('c:\xxx\yyy')
> import zzz
> 
> (Untested, similar idiom would work in *nix systems, never programmed in
> Windows)
> 
> However, I guess it is not very usual you should need to import stuff from
> arbitrary locations. Consider publishing those modules in normal Python
> include path (just see what ''print sys.path'' produces)
> 
> -- 
> # Edvard Majakari		Software Engineer
> # PGP PUBLIC KEY available    	Soli Deo Gloria!
> 
> $_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
> join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list