.pth - howto?

F

flippetigibbet

I was thinking that if I create:
\documents and settings\user\My
Documents\my\scripts\py\dirname\__init__.py
and a .pth file in site-packages with something like:
\documents and settings\user\My Documents\my\scripts\py\dirname
then my script __init__.py should run when I import dirname
but it doesn't

My team of trigger-happy lawyers is currently analyzing "Installing
Python Modules", which says:
Paths can be absolute or relative, in which case they're relative to
the directory containing the .pth file.

I've tried specifying the .pth file with
r'\documents and settings\user\My Documents\my\scripts\py\dirname'
\DOCUME~1\user\MyDocu~1\my\scripts\py\dirname
/doceme~1/user/MYDOCU~1/MY/SCRIPTS/PY/DIRNAME
but I can't seem to get python 2.4 to pick up an absolute path on
windows.

I know I could put my scripts in site-packages, but I quite like the
idea of keeping my scripts and 3rd party scripts separate. Also, I
would prefer not to add "\documents and settings\user\My
Documents\my\scripts\py" to the PYTHONPATH, because as soon as I
install some 3rd party, I tend to write a 'play' script to run it, so I
only want the stuff I specify on the path.

So how do I configure an absolute path in a .pth file on windows?
 
P

Peter Hansen

flippetigibbet said:
I've tried specifying the .pth file with
r'\documents and settings\user\My Documents\my\scripts\py\dirname'
\DOCUME~1\user\MyDocu~1\my\scripts\py\dirname
/doceme~1/user/MYDOCU~1/MY/SCRIPTS/PY/DIRNAME
but I can't seem to get python 2.4 to pick up an absolute path on
windows.

Does that directory actually exist? It won't get added if it doesn't
exist. You can also just trace through the source in "site.py" as it
loads (by inserting an "import pdb; pdb.set_trace()" near the top), or
add some print statements at key points to see what it's doing.

(Note that your first example above definitely wouldn't work, since that
is the syntax for a string in Python source, whereas the relevant
function [i.e. addpackage()] is merely reading lines from a file...)

-Peter
 
F

flippetigibbet

I inserted the 'import pdb...' suggestions and set the .pth back to the
first thing I'd tried:
C:\Documents and Settings\user\My Documents\my\scripts\py\mydir
and lo and behold - it works!!

Then I took out the "import pdb ...' and ... it still works!!

What did you do to my system to get it to work? :)

Seriously though: thank you very much. I guess there must have been a
typo in my original attempts or something like that. My team of
trigger-happy lawyers are looking at me and licking their lips.
 
F

flippetigibbet

I've just realized. I dropped a copy of my script in site-packages, in
order to get it working. When I remove that, I'm back to the orginal
problem.

If I add the pdb tracing in site.py, I reply 'r' as it enters each
function, then I try and import my module and ...Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named textfile
(textfile is actually the name of the module, whereas I've been using
'dirname' upto now, in an effort to make things clearer, but probably
confusing things even more)
 
F

flippetigibbet

Ok, I think I've finally worked out where my error was. My .pth file
had:
C:\Documents and Settings\user\My Documents\my\scripts\py\mydir
and I was importing mydir, but that's wrong.

I changed my .pth to have:
C:\Documents and Settings\user\My Documents\my\scripts\py
and imported mydir and it worked just fine

I think I should have just done print sys.path at a python prompt
before posting - I think that would have shown why it wasn't working
(though thankyou again Peter, for without your suggestion, I wouldn't
have got there).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top