.pth files

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

    .pth files

    Hi there !

    I've some questions regarding pth files (which btw are undocumented in the
    python reference, is this intentional ?)

    I thought that I could use a .pth file to be able to import zope products
    from both INSTANCE_HOME/Products and ZOPE_HOME/lib/python/Products from
    outside zope:


    syt@musca:~$ cat cvs_work/Products.pth
    /home/syt/local/Zope-2.8.1-b1/Products
    /home/syt/local/Zope-2.8.1-b1/lib/python/Products
    syt@musca:~$
    syt@musca:~$ python
    Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
    [GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import sys
    >>> print sys.path[/color][/color][/color]
    ['', '/home/syt/cvs_work', '/home/syt/cvs_work/prive/soft',
    '/home/syt/local/lib/python2.3/site-packages',
    '/home/syt/local/lib/python', '/usr/lib/python23.zip',
    '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
    '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
    '/usr/local/lib/python2.3/site-packages',
    '/usr/lib/python2.3/site-packages',
    '/usr/lib/python2.3/site-packages/Numeric',
    '/usr/lib/python2.3/site-packages/PIL',
    '/usr/lib/python2.3/site-packages/gtk-2.0',
    '/usr/lib/python2.3/site-packages/vtk_python', '/usr/lib/site-python']

    But as you can see,
    1. the Products.pth file isn't considered at all, while for example
    PIL.pht in the site-packages is correctly detected
    2. I'm not even sure that I can put several paths in a .pth file

    Is there a restriction on .pth location ? Is it possible to have multiple
    path in a pth file ?

    --
    Sylvain Thénault LOGILAB, Paris (France).

    http://www.logilab.com http://www.logilab.fr http://www.logilab.org


  • Adriano Varoli Piazza

    #2
    Re: .pth files

    Sylvain Thenault ha scritto:[color=blue]
    > Hi there !
    >
    > I've some questions regarding pth files (which btw are undocumented in the
    > python reference, is this intentional ?)
    >
    > I thought that I could use a .pth file to be able to import zope products
    > from both INSTANCE_HOME/Products and ZOPE_HOME/lib/python/Products from
    > outside zope:
    >
    >
    > syt@musca:~$ cat cvs_work/Products.pth
    > /home/syt/local/Zope-2.8.1-b1/Products
    > /home/syt/local/Zope-2.8.1-b1/lib/python/Products
    > syt@musca:~$
    > syt@musca:~$ python
    > Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
    > [GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2
    > Type "help", "copyright" , "credits" or "license" for more information.
    >[color=green][color=darkred]
    >>>>import sys
    >>>>print sys.path[/color][/color]
    >
    > ['', '/home/syt/cvs_work', '/home/syt/cvs_work/prive/soft',
    > '/home/syt/local/lib/python2.3/site-packages',
    > '/home/syt/local/lib/python', '/usr/lib/python23.zip',
    > '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
    > '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
    > '/usr/local/lib/python2.3/site-packages',
    > '/usr/lib/python2.3/site-packages',
    > '/usr/lib/python2.3/site-packages/Numeric',
    > '/usr/lib/python2.3/site-packages/PIL',
    > '/usr/lib/python2.3/site-packages/gtk-2.0',
    > '/usr/lib/python2.3/site-packages/vtk_python', '/usr/lib/site-python']
    >
    > But as you can see,
    > 1. the Products.pth file isn't considered at all, while for example
    > PIL.pht in the site-packages is correctly detected
    > 2. I'm not even sure that I can put several paths in a .pth file
    >
    > Is there a restriction on .pth location ? Is it possible to have multiple
    > path in a pth file ?
    >[/color]

    From Learning Python, 2nd Ed:
    "a relatively new feature of Python allows users to add valid
    directories to the module search path by simply listing them, one per
    line, in a text file whose name ends in a .pth suffix.

    See also the docs for the site module in the Python Library reference.

    --
    Adriano Varoli Piazza
    The Inside Out: http://moranar.com.ar
    MSN: adrianomd@hotma il.com
    ICQ: 4410132

    Comment

    • Sylvain Thenault

      #3
      Re: .pth files

      On Tue, 09 Aug 2005 09:37:47 +0000, Adriano Varoli Piazza wrote:
      [color=blue]
      > Sylvain Thenault ha scritto:[color=green]
      >> Hi there !
      >>
      >> I've some questions regarding pth files (which btw are undocumented in
      >> the python reference, is this intentional ?)
      >>
      >> I thought that I could use a .pth file to be able to import zope
      >> products from both INSTANCE_HOME/Products and
      >> ZOPE_HOME/lib/python/Products from outside zope:
      >>
      >>
      >> syt@musca:~$ cat cvs_work/Products.pth
      >> /home/syt/local/Zope-2.8.1-b1/Products
      >> /home/syt/local/Zope-2.8.1-b1/lib/python/Products syt@musca:~$
      >> syt@musca:~$ python
      >> Python 2.3.5 (#2, Jun 19 2005, 13:28:00) [GCC 3.3.6 (Debian 1:3.3.6-6)]
      >> on linux2 Type "help", "copyright" , "credits" or "license" for more
      >> information.
      >>[color=darkred]
      >>>>>import sys
      >>>>>print sys.path[/color]
      >>
      >> ['', '/home/syt/cvs_work', '/home/syt/cvs_work/prive/soft',
      >> '/home/syt/local/lib/python2.3/site-packages',
      >> '/home/syt/local/lib/python', '/usr/lib/python23.zip',
      >> '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
      >> '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
      >> '/usr/local/lib/python2.3/site-packages',
      >> '/usr/lib/python2.3/site-packages',
      >> '/usr/lib/python2.3/site-packages/Numeric',
      >> '/usr/lib/python2.3/site-packages/PIL',
      >> '/usr/lib/python2.3/site-packages/gtk-2.0',
      >> '/usr/lib/python2.3/site-packages/vtk_python', '/usr/lib/site-python']
      >>
      >> But as you can see,
      >> 1. the Products.pth file isn't considered at all, while for example
      >> PIL.pht in the site-packages is correctly detected
      >> 2. I'm not even sure that I can put several paths in a .pth file
      >>
      >> Is there a restriction on .pth location ? Is it possible to have
      >> multiple path in a pth file ?
      >>
      >>[/color]
      > From Learning Python, 2nd Ed:
      > "a relatively new feature of Python allows users to add valid directories
      > to the module search path by simply listing them, one per line, in a text
      > file whose name ends in a .pth suffix.
      >
      > See also the docs for the site module in the Python Library reference.[/color]

      ha, so that's where it's documented !
      so answer are:
      1. Products.pth are only considered in standard site-packages and
      site-python directories
      2. yes, it's possible

      Now, the question become: why can't we use pth files in other path
      specified by the PYTHONPATH environement variable ?

      --
      Sylvain Thénault LOGILAB, Paris (France).

      http://www.logilab.com http://www.logilab.fr http://www.logilab.org


      Comment

      • Richie Hindle

        #4
        Re: .pth files


        [Sylvain][color=blue]
        > I've some questions regarding pth files (which btw are undocumented in the
        > python reference, is this intentional ?)[/color]



        The first hit explains how .pth files work (although it's the sort of
        documentation that makes Xah Lee explode with fury).

        --
        Richie Hindle
        richie@entrian. com

        Comment

        • Erik Max Francis

          #5
          Re: .pth files

          Richie Hindle wrote:
          [color=blue]
          > http://google.com/search?q=site:docs.python.org%20pth
          >
          > The first hit explains how .pth files work (although it's the sort of
          > documentation that makes Xah Lee explode with fury).[/color]

          That just makes it all the more delicious.

          --
          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
          There is another world, which is not of men.
          -- Li Bai

          Comment

          • Neil Benn

            #6
            Re: .pth files

            Sylvain Thenault wrote:
            [color=blue]
            >On Tue, 09 Aug 2005 09:37:47 +0000, Adriano Varoli Piazza wrote:
            >
            >
            >[color=green]
            >>Sylvain Thenault ha scritto:
            >>
            >>[color=darkred]
            >>>Hi there !
            >>>
            >>>I've some questions regarding pth files (which btw are undocumented in
            >>>the python reference, is this intentional ?)
            >>>
            >>>I thought that I could use a .pth file to be able to import zope
            >>>products from both INSTANCE_HOME/Products and
            >>>ZOPE_HOME/lib/python/Products from outside zope:
            >>>
            >>>
            >>>syt@musca: ~$ cat cvs_work/Products.pth
            >>>/home/syt/local/Zope-2.8.1-b1/Products
            >>>/home/syt/local/Zope-2.8.1-b1/lib/python/Products syt@musca:~$
            >>>syt@musca: ~$ python
            >>>Python 2.3.5 (#2, Jun 19 2005, 13:28:00) [GCC 3.3.6 (Debian 1:3.3.6-6)]
            >>>on linux2 Type "help", "copyright" , "credits" or "license" for more
            >>>informatio n.
            >>>
            >>>
            >>>
            >>>>>>import sys
            >>>>>>print sys.path
            >>>>>>
            >>>>>>
            >>>['', '/home/syt/cvs_work', '/home/syt/cvs_work/prive/soft',
            >>> '/home/syt/local/lib/python2.3/site-packages',
            >>> '/home/syt/local/lib/python', '/usr/lib/python23.zip',
            >>> '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
            >>> '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
            >>> '/usr/local/lib/python2.3/site-packages',
            >>> '/usr/lib/python2.3/site-packages',
            >>> '/usr/lib/python2.3/site-packages/Numeric',
            >>> '/usr/lib/python2.3/site-packages/PIL',
            >>> '/usr/lib/python2.3/site-packages/gtk-2.0',
            >>> '/usr/lib/python2.3/site-packages/vtk_python', '/usr/lib/site-python']
            >>>
            >>>But as you can see,
            >>>1. the Products.pth file isn't considered at all, while for example
            >>> PIL.pht in the site-packages is correctly detected
            >>>2. I'm not even sure that I can put several paths in a .pth file
            >>>
            >>>Is there a restriction on .pth location ? Is it possible to have
            >>>multiple path in a pth file ?
            >>>
            >>>
            >>>
            >>>[/color]
            >> From Learning Python, 2nd Ed:
            >>"a relatively new feature of Python allows users to add valid directories
            >>to the module search path by simply listing them, one per line, in a text
            >>file whose name ends in a .pth suffix.
            >>
            >>See also the docs for the site module in the Python Library reference.
            >>
            >>[/color]
            >
            >ha, so that's where it's documented !
            >so answer are:
            >1. Products.pth are only considered in standard site-packages and
            > site-python directories
            >2. yes, it's possible
            >
            >Now, the question become: why can't we use pth files in other path
            >specified by the PYTHONPATH environement variable ?
            >
            >
            >[/color]
            Hello,

            I've been doing some work on that recently, if your do want to
            include .pth files specified by a different location then you have to do
            some work, here are a couple of solutions:

            * Site.py is prompted to load a sitecustomise.p y file which is
            located in the Lib directory - if it is there. This file is
            automatically executed on the start up of python. Here you can
            look for a command line argument (or a local config file) passed
            in - you can then go and find the .pth file yourself. The code to
            pull in .pth stuff is located in the site.py file - you can import
            this function and use it but I don't do that for two reasons
            (instead I pull across my own copy - effectivly freezing that
            implementation) :

            1. You are creating a circular reference
            2. I can find no docs or imformation about using the functions
            in the site.py file so therefore I have to assume that the
            functions in the site.py file are not designed to be used
            this way and therefore shouldn't be relied upon to be stable
            or even present across python releases (I'm a conservative
            programmer.

            This however can be problematic if you are managing a large
            number of client installs as you will need to have a local file on the
            local box - thereby making upgrading the clients installs difficult.

            * I created a python module which can receive command line arguments
            setting up site-package directories and pythonpath which executes
            as a file, this also receives an argument of which python file to
            execute after this. This means that the file which does this work
            can be located anywhere (ie on your company server) - making
            release management easier. However this solution requires more work.
            * You could copy the java method using zip files with a manifest in
            the same way Java uses jar files - I say could because I havn't
            done this but it is the way I distribute Java apps - nice and
            clean, also installed my own JRE with my program - that way I have
            complete control (anyone that complains about using hard drive
            space can go back to using their ZX81 :-)) over my environment.


            If you are not worried about managing the release stuff and
            sitecustomise works fine for you then this is the way to go as it is the
            simplest (less code means more reliable). However be aware that the
            sitepackage directory on the python install will automatically be
            included and you could have two versions of the same package in
            sys.path. Finally, one last thing - beware if you are on windows as
            some path information is written in the registry and this is included
            automatically.

            Cheers,

            Neil

            --

            Neil Benn
            Senior Automation Engineer
            Cenix BioScience
            BioInnovations Zentrum
            Tatzberg 47
            D-01307
            Dresden
            Germany

            Tel : +49 (0)351 4173 154
            e-mail : benn@cenix-bioscience.com
            Cenix Website : http://www.cenix-bioscience.com

            Comment

            • Peter Hansen

              #7
              Re: .pth files

              Neil Benn wrote:[color=blue]
              > * Site.py is prompted to load a sitecustomise.p y file which is
              > located in the Lib directory - if it is there. This file is
              > automatically executed on the start up of python. Here you can
              > look for a command line argument (or a local config file) passed
              > in - you can then go and find the .pth file yourself. The code to
              > pull in .pth stuff is located in the site.py file - you can import
              > this function and use it but I don't do that for two reasons
              > (instead I pull across my own copy - effectivly freezing that
              > implementation) :
              >
              > 1. You are creating a circular reference[/color]

              The "import sitecustomize" stuff is placed effectively at the end of
              site.py, so it should be quite safe to "import site" from your
              sitecustomize.p y file and use the functions there.
              [color=blue]
              > 2. I can find no docs or imformation about using the functions
              > in the site.py file so therefore I have to assume that the
              > functions in the site.py file are not designed to be used
              > this way and therefore shouldn't be relied upon to be stable
              > or even present across python releases (I'm a conservative
              > programmer.[/color]

              While I can't fault your caution, we've been using a few things in
              site.py from sitecustomize for a while with no problems (across releases
              from Python 1.5.2 to Python 2.4). The functions are documented in the
              site.py source itself, of course, at least in a cursory fashion, and
              while there have been changes over versions, I don't recall anything
              causing breakage. Normally I'd quite agree about staying away, but for
              something like this (i.e. not a core part of an application, but
              something to set up the environment instead) I'd say "go for it".

              -Peter

              Comment

              Working...