.pth - howto?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • flippetigibbet

    #1

    .pth - howto?

    I was thinking that if I create:
    \documents and settings\user\M y
    Documents\my\sc ripts\py\dirnam e\__init__.py
    and a .pth file in site-packages with something like:
    \documents and settings\user\M y Documents\my\sc ripts\py\dirnam e
    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\M y Documents\my\sc ripts\py\dirnam e'
    \DOCUME~1\user\ MyDocu~1\my\scr ipts\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\M y
    Documents\my\sc ripts\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?

  • Peter Hansen

    #2
    Re: .pth - howto?

    flippetigibbet wrote:[color=blue]
    > I've tried specifying the .pth file with
    > r'\documents and settings\user\M y Documents\my\sc ripts\py\dirnam e'
    > \DOCUME~1\user\ MyDocu~1\my\scr ipts\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.[/color]

    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

    Comment

    • Peter Hansen

      #3
      Re: .pth - howto?

      flippetigibbet wrote:[color=blue]
      > I've tried specifying the .pth file with
      > r'\documents and settings\user\M y Documents\my\sc ripts\py\dirnam e'
      > \DOCUME~1\user\ MyDocu~1\my\scr ipts\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.[/color]

      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

      Comment

      • flippetigibbet

        #4
        Re: .pth - howto?

        I inserted the 'import pdb...' suggestions and set the .pth back to the
        first thing I'd tried:
        C:\Documents and Settings\user\M y Documents\my\sc ripts\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.

        Comment

        • flippetigibbet

          #5
          Re: .pth - howto?

          I inserted the 'import pdb...' suggestions and set the .pth back to the
          first thing I'd tried:
          C:\Documents and Settings\user\M y Documents\my\sc ripts\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.

          Comment

          • flippetigibbet

            #6
            Re: .pth - howto?

            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 ...[color=blue][color=green][color=darkred]
            >>> import textfile[/color][/color][/color]
            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)

            Comment

            • flippetigibbet

              #7
              Re: .pth - howto?

              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 ...[color=blue][color=green][color=darkred]
              >>> import textfile[/color][/color][/color]
              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)

              Comment

              • flippetigibbet

                #8
                Re: .pth - howto?

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

                I changed my .pth to have:
                C:\Documents and Settings\user\M y Documents\my\sc ripts\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).

                Comment

                • flippetigibbet

                  #9
                  Re: .pth - howto?

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

                  I changed my .pth to have:
                  C:\Documents and Settings\user\M y Documents\my\sc ripts\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).

                  Comment

                  Working...