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.
4 Answers 4
One way is to simply amend your path:
import sys
sys.path.append('C:/full/path')
from foo import util,bar
Note that this requires foo to be a python package, i.e. contain a __init__.py file. If you don't want to modify sys.path, you can also modify the PYTHONPATH environment variable or install the module on your system. Beware that this means that other directories or .py files in that directory may be loaded inadvertently.
Therefore, you may want to use imp.load_source instead. It needs the filename, not a directory (to a file which the current user is allowed to read):
import imp
util = imp.load_source('util', 'C:/full/path/foo/util.py')
10 Comments
method, only "the method in util". You can define method = util.method if you want.__init__.py is required. Observe mkdir foo; touch foo/util.py; python -c 'from foo import util' and touch foo/__init__.py; python -c 'from foo import util'imp.load_source because the user may not want to modify any system paths, or may be working on a computer where she or he cannot do so. This is actually one of the most annoying "features" in Python, which otherwise is an elegant language. It's silly that you can't just give import a string naming a path to another .py file. And given that you can't, protecting against changing system paths by using imp is strictly better.sys.path. If the user cannot modify sys.path, it's extremely likely that the imp module is blocked as well (note that Python's sys.path has nothing with the system's PATH). Personally, I don't think that loading modules by name instead of filename is silly; it allows system-wide installations of Python packages (for example, on 2.5 you may want to serve your alternative json module). Added a cautionary note.You could customize the module search path using the PYTHONPATH environment variable, or manually modify the sys.path directory list.
See Module Search Path documentation on python.org.
Comments
Give this a try
import sys
sys.path.append('c:/.../dir/dir2')
import foo
6 Comments
sys.path (or at least an intended use case). Mr. F (who apparently was previously EMS) is wrong. As phihag notes in his response to the identical -1 comment on his answer, sys.path is not related to the overall system path.Following phihag's tip, I have this solution. Just give the path of a source file to load_src and it will load it. You must also provide a name, so you can import this module using this name. I prefer to do it this way because it's more explicit:
def load_src(name, fpath):
import os, imp
return imp.load_source(name, os.path.join(os.path.dirname(__file__), fpath))
load_src("util", "../util.py")
import util
print util.method()
Another (less explicit) way is this:
util = load_src("util", "../util.py") # "import util" is implied here
print util.method() # works, util was imported by the previous line
Edit: the method is rewritten to make it clearer.
1 Comment
imp has been deprecated since mid-2014 and no longer exists in Python 3.12.Explore related questions
See similar questions with these tags.
sys.path.