allow scripts to use .pth files?
samwyse
samwyse at gmail.com
Thu Jul 12 07:55:09 EDT 2007
On Jul 8, 3:53 pm, John Machin <sjmac... at lexicon.net> wrote:
> I got the impression that the OP was suggesting that the interpreter
> look in the directory in which it found the script.
[...]
> I got the impression that the problem was that the package was not
> only not on sys.path but also not in the same directory as the script
> that wanted to import it. Otherwise the OP's script.p?h file need only
> contain ".\n" (or the path to the directory in which it resided!!),
> and I doubt that he was proposing something so silly.
And as I'm sure you realize, those two impression slightly contradict
each other. Anyway, a small modification to my second approach would
also work in the case looking for packages in a directory that's
located somewhere relative to the one where the script was found:
if __name__ == '__main__':
import sys, os.path
base = sys.path[0]
for d in 'lib', 'modules':
d2 = os.path.join(base, d)
if os.path.isdir(d2):
sys.path.append(d2)
base = os.path.join(base, '..')
for d in 'modules', 'lib':
d2 = os.path.join(base, d)
if os.path.isdir(d2):
sys.path.append(d2)
# for debugging
print repr(sys.path)
(BTW, this is such a fun script to type. My fingers keep typing
'os.path' where I mean 'sys.path' and vice versa.)
More information about the Python-list
mailing list