473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

allow scripts to use .pth files?

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
8 2362
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
6
by: B | last post by:
Hi, I'm developing a web application where users can upload multiple files to the website. I'm using asp.net 2.0 with FileUpload control and can upload 1 image to the server, but I would like...
3
by: Bouffa | last post by:
Hello everyone, I suppose you all know force-download scripts. The problem is that these scripts don't allow files to be splitted when downloading them via a download manager. I've found a...
3
by: djc | last post by:
I have used the FileUpload web server control to upload a file in a previous project. Now I'm wondering how I can allow a user to select more than one file for upload and upload them at the same...
3
by: Jude | last post by:
IIS on my local machine serves both ASP and ASP.NET pages fine but when I debug using the built in Development Server that comes with Visual Studio 2005 I get the following error when I try to...
3
chunk1978
by: chunk1978 | last post by:
hi there... i have a form where a user may optionally upload a maximum of 2 files along with other textual data (there are 2 file fields built in the form). i'm having trouble writing a php script...
2
by: subbar443 | last post by:
Hi I had written a perl file.From this perl file i want to call .csh scripts. We have to set any environment variables in perl file for this purpose. Could you give me suggestions what are all...
19
by: Robert Kochem | last post by:
Hi, I am relative new to C++ regarding it's functions and libraries. I need to access files larger than 4GB which is AFAIK not possible with the STL iostream - at least not if using a 32 bit...
7
by: Bobby Edward | last post by:
Are there any ASP.NET compatible controls out there that will allow you to upload large files, up to 2 gb? Prefer free of course. ;)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.