There was an answer to this in this question (How to import a module given the full path?). It is the second answer with like 400 upvotes. However, the answer was never edited to include what many people commented asking for.
In the answer he says to do this:
'The advantage of adding a path to sys.path (over using imp) is that it simplifies things when importing more than one module from a single package. For example:
import sys
# the mock-0.3.1 dir contains testcase.py, testutils.py & mock.py
sys.path.append('/foo/bar/mock-0.3.1')
from testcase import TestCase
from testutils import RunTests
from mock import Mock, sentinel, patch
'
What many people were asking was basically how do we use sys.path.append
to point to a single python file instead of a directory?
Thanks in advance!
You can't. sys.path
doesn't work that way. It's a list of directories to look in, not files to look at.