how do I import functions from a file in the same directory as the file that is the source of the current text buffer? I have a text buffer that is loaded from an external file.  In that same text buffer I need to write code that imports some functions from another file in the same directory as the one in the text buffer.


Hardcoding that path in the source is unsuitable because the file may live in different directories on different users' machines.
Command-line arguments seem like a a horrid Rube Goldberg kludge and a horrible user experience.
a file selection dialog is also poor user experience.


Java has a mechanism similar to what I want using class.getResource(relPath) (imagine if tomcat gave you a file selection dialog box when it needed to load the Spanish translations for localized string resources)

What is the best python technique to accomplish this?
You can add the text file's folder to the system path, which will enable you to import from that path.

I'd also suggest to extract the folder path via os.path.dirname.

Short example to clarify:

import sys
from os.path import dirname
textFilePath = "/home/me/somewhere/textfiles/text1"
textFileFolder = dirname( textFilePath ) # = "/home/me/somewhere/textfiles"

sys.path.append(textFileFolder)

import someFileThatExistsInTextFolder