By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

allow scripts to use .pth files?

P: n/a
Suppose I have a directory `scripts`.
I'd like the scripts to have access to a package
that is not "installed", i.e., it is not on sys.path.
On this list, various people have described a variety
of tricks they use, but nobody has proposed a
pretty way to allow this.
I am therefore assuming there is not one. (?)

How about allowing a `scripts.pth` file in such a `scripts`
directory, to work like a path configuration file?
(But to be used only when __name__=="__main__".)
Drawbacks?

Alan Isaac
Jul 3 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Jul 3, 7:35 am, Alan Isaac <ais...@american.eduwrote:
Suppose I have a directory `scripts`.
I'd like the scripts to have access to a package
that is not "installed", i.e., it is not on sys.path.
On this list, various people have described a variety
of tricks they use, but nobody has proposed a
pretty way to allow this.
I am therefore assuming there is not one. (?)

How about allowing a `scripts.pth` file in such a `scripts`
directory, to work like a path configuration file?
(But to be used only when __name__=="__main__".)
Drawbacks?

Alan Isaac
import sys
sys.path.append("../scripts")

import Module_from_scripts_dir

~Sean

Jul 3 '07 #2

P: n/a
On Jul 3, 7:35 am, Alan Isaac <ais...@american.eduwrote:
>>Suppose I have a directory `scripts`.
I'd like the scripts to have access to a package
that is not "installed", i.e., it is not on sys.path.
On this list, various people have described a variety
of tricks they use, but nobody has proposed a
pretty way to allow this.
I am therefore assuming there is not one. (?)

How about allowing a `scripts.pth` file in such a `scripts`
directory, to work like a path configuration file?
(But to be used only when __name__=="__main__".)
Drawbacks?


ha**********@gmail.com wrote:
import sys
sys.path.append("../scripts")
import Module_from_scripts_dir

That is not actually an answer to the question. In any case,
path manipulation in scripts is often pronounced "not pretty".
(And would also have to be done in every script file.)

Cheers,
Alan Isaac
Jul 5 '07 #3

P: n/a
On Jul 3, 9:35 am, Alan Isaac <ais...@american.eduwrote:
Suppose I have a directory `scripts`.
I'd like the scripts to have access to a package
that is not "installed", i.e., it is not on sys.path.
On this list, various people have described a variety
of tricks they use, but nobody has proposed a
pretty way to allow this.
I am therefore assuming there is not one. (?)

How about allowing a `scripts.pth` file in such a `scripts`
directory, to work like a path configuration file?
(But to be used only when __name__=="__main__".)
Drawbacks?

Alan Isaac
before i can adequately shoot down your proposal, i need more
information.

first, how does the interpreter know to look in 'scripts' for your
'scripts.pyh' file and not in '.' or '~' or sys.argv[0] or someplace
else? and you do know, don't you, that if you run 'scripts/
myscript.py' then 'scripts' is automagically prepended to the search
path for modules?

second, what's for format of this proposed file? does it contain the
name of a single directory? is it one directory name per line? is it
intended to be os-specific or will the same file work on windows,
unix, vms and macos?

third, is there anything special about the name? should i put a
myscripts.pyh file in the myscripts directory? what if i have
multiple .pyh files?

if i may make some assumptions about the answers to the above, then
this incantation might do somewhat more than what you've asked for
(but what you actually want may be different):

if __name__ == '__main__':
import sys
from os.path import split, join, expanduser
for d in '.', split(sys.argv[0])[0], expanduser('~'):
scripts_pyh = join(d, 'scripts.pyh')
try:
for each_line in open(scripts_pyh).readlines():
sys.path.append(each_line)
except:
pass
personally, though, i would be more likely to use this and skip all of
that 'scripts.pyh' nonsense:

if __name__ == '__main__':
import sys, os.path
for d in '.', os.path.expanduser('~'):
if os.path.isdir(d): sys.path.append(d)

Jul 8 '07 #4

P: n/a
On Jul 8, 10:53 pm, samwyse <samw...@gmail.comwrote:
On Jul 3, 9:35 am, Alan Isaac <ais...@american.eduwrote:
Suppose I have a directory `scripts`.
I'd like the scripts to have access to a package
that is not "installed", i.e., it is not on sys.path.
On this list, various people have described a variety
of tricks they use, but nobody has proposed a
pretty way to allow this.
I am therefore assuming there is not one. (?)
How about allowing a `scripts.pth` file in such a `scripts`
directory, to work like a path configuration file?
(But to be used only when __name__=="__main__".)
Drawbacks?
Alan Isaac

before i can adequately shoot down your proposal, i need more
information.

first, how does the interpreter know to look in 'scripts' for your
'scripts.pyh' file and not in '.' or '~' or sys.argv[0] or someplace
else?
I got the impression that the OP was suggesting that the interpreter
look in the directory in which it found the script.
and you do know, don't you, that if you run 'scripts/
myscript.py' then 'scripts' is automagically prepended to the search
path for modules?
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.

I'm curious whether you think that the OP's use of ".pth" was a typo,
and whether you have read this:
http://docs.python.org/lib/module-site.html

Cheers,
John

Jul 8 '07 #5

P: n/a
John Machin wrote:
I got the impression that the OP was suggesting that the interpreter
look in the directory in which it found the script.
Right.

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.
Right.

I'm curious whether you think that the OP's use of ".pth" was a typo,
and whether you have read this:
http://docs.python.org/lib/module-site.html
You seem to understand what I'm getting at.
Thanks John.

Alan Isaac (the OP above)
Jul 10 '07 #6

P: n/a
On Jul 8, 3:53 pm, John Machin <sjmac...@lexicon.netwrote:
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.)

Jul 12 '07 #7

P: n/a
On Jul 12, 9:55 pm, samwyse <samw...@gmail.comwrote:
On Jul 8, 3:53 pm, John Machin <sjmac...@lexicon.netwrote:
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.
Your sureness is misplaced. Please explain.

Jul 12 '07 #8

P: n/a
On Jul 12, 7:20 am, John Machin <sjmac...@lexicon.netwrote:
On Jul 12, 9:55 pm, samwyse <samw...@gmail.comwrote:
On Jul 8, 3:53 pm, John Machin <sjmac...@lexicon.netwrote:
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.

Your sureness is misplaced. Please explain.

Oops, you're right. See my other post.

Jul 12 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.