I have a module foo, containing util.py and bar.py.
I want to import it in IDLE or python session. How do I go about this?
I could find no documentation on how to import modules not in the current directory or the default python PATH.
After trying import "<full path>/foo/util.py",
and from "<full path>" import util
The closest I could get was
import imp
imp.load_source('foo.util','C:/.../dir/dir2/foo')
Which gave me Permission denied on windows 7.
One way is to simply amend your path:
Note that this requires foo to be a python package, i.e. contain a
__init__.pyfile. If you don’t want to modifysys.path, you can also modify thePYTHONPATHenvironment variable or install the module on your system. Beware that this means that other directories or.pyfiles in that directory may be loaded inadvertently.Therefore, you may want to use
imp.load_sourceinstead. It needs the filename, not a directory (to a file which the current user is allowed to read):