Accessing files installed with distutils

Discussion in 'Python' started by Frans Englich, Feb 21, 2005.

  1. This is silly. How do I access data files I've installed with distutils? In a
    portable, generic way, I want to find out what is the following path on most
    systems:

    /usr/local/lib/python2.4/lib/site-packages/foo/bar.txt

    How do I figure out the rest, if I know foo/bar.txt? sys.prefix doesn't get me
    far.

    I've googled, and looked in the python reference. I must be blind if the
    distutils section[1] covers this.


    Cheers,

    Frans

    1.
    http://www.python.org/doc/current/dist/dist.html
    Frans Englich, Feb 21, 2005
    #1
    1. Advertising

  2. Frans Englich

    John Machin Guest

    Frans Englich wrote:
    > This is silly. How do I access data files I've installed with

    distutils? In a
    > portable, generic way, I want to find out what is the following path

    on most
    > systems:
    >
    > /usr/local/lib/python2.4/lib/site-packages/foo/bar.txt


    Most systems? A tad *nix-centric, yes/no?

    >
    > How do I figure out the rest, if I know foo/bar.txt? sys.prefix

    doesn't get me
    > far.


    This hint may be useful:

    >>> import sys
    >>> sys.modules['readline'].__file__

    'c:\\Python24\\lib\\site-packages\\readline\\__init__.pyc'

    but it may not work if the caller does "from yourmodule import ....."
    or "import yourmodule as ym". Over to you.
    John Machin, Feb 22, 2005
    #2
    1. Advertising

  3. Frans Englich wrote:
    > This is silly. How do I access data files I've installed with distutils? In a
    > portable, generic way, I want to find out what is the following path on most
    > systems:
    >
    > /usr/local/lib/python2.4/lib/site-packages/foo/bar.txt


    Assuming your module is also in site-packages/foo, I would use:

    import os
    filename = os.path.join(os.path.dirname(__file__), 'bar.txt')
    Leif K-Brooks, Feb 22, 2005
    #3
  4. Frans Englich

    Guest

    I was wondering how to do this too. I'm trying to write a distutils
    setup.py script that has some data I'd like to include. From the
    distutils docs I get

    data_files specifies a sequence of (directory, files) pairs in the
    following way:

    setup(...
    data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
    ('config', ['cfg/data.cfg']),
    ('/etc/init.d', ['init-script'])]
    )

    I can use sys.prefix to find the top level where python is installed,
    but this doesn't tell me specifically where site-packages is. On my
    Linux box it is in

    {sys.prefix}/lib/python2.3/site-packages

    but on Windows, it's in

    {sys.prefix}/Lib/site-packages.

    Do I need to use sys.platform (along with sys.version) to check what
    type of machine I'm on, or is there some better method to get the
    location of site-packages?

    Thanks.
    , Mar 8, 2005
    #4
  5. Frans Englich

    Steve Holden Guest

    wrote:
    > I was wondering how to do this too. I'm trying to write a distutils
    > setup.py script that has some data I'd like to include. From the
    > distutils docs I get
    >
    > data_files specifies a sequence of (directory, files) pairs in the
    > following way:
    >
    > setup(...
    > data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
    > ('config', ['cfg/data.cfg']),
    > ('/etc/init.d', ['init-script'])]
    > )
    >
    > I can use sys.prefix to find the top level where python is installed,
    > but this doesn't tell me specifically where site-packages is. On my
    > Linux box it is in
    >
    > {sys.prefix}/lib/python2.3/site-packages
    >
    > but on Windows, it's in
    >
    > {sys.prefix}/Lib/site-packages.
    >
    > Do I need to use sys.platform (along with sys.version) to check what
    > type of machine I'm on, or is there some better method to get the
    > location of site-packages?
    >

    This is one of the areas where distutils could probably do with some
    improvement. I don;t know whether it's on any developers priority list,
    though.

    regards
    Steve
    Steve Holden, Mar 8, 2005
    #5
  6. Steve Holden <> writes:

    > wrote:
    >> I was wondering how to do this too. I'm trying to write a distutils
    >> setup.py script that has some data I'd like to include. From the
    >> distutils docs I get
    >> data_files specifies a sequence of (directory, files) pairs in the
    >> following way:
    >> setup(...
    >> data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
    >> ('config', ['cfg/data.cfg']),
    >> ('/etc/init.d', ['init-script'])]
    >> )
    >> I can use sys.prefix to find the top level where python is installed,
    >> but this doesn't tell me specifically where site-packages is. On my
    >> Linux box it is in
    >> {sys.prefix}/lib/python2.3/site-packages
    >> but on Windows, it's in
    >> {sys.prefix}/Lib/site-packages.
    >> Do I need to use sys.platform (along with sys.version) to check what
    >> type of machine I'm on, or is there some better method to get the
    >> location of site-packages?
    >>

    > This is one of the areas where distutils could probably do with some
    > improvement. I don;t know whether it's on any developers priority
    > list, though.


    There are some functions in distutils.sysconfig which may help.

    Thomas
    Thomas Heller, Mar 8, 2005
    #6
  7. Frans Englich

    Guest

    I ended up using the trick I found in the Disutils Cookbook.

    http://www.python.org/moin/DistutilsInstallDataScattered

    This works fine for me now, but I have another distutils question.

    My package requires Pmw and another home grown package that has a
    source dist and a Windows dist built with the bdist_wininst command.
    Is there a way to include the tarballs and *.win32-py2.3.exe files to
    install them if necessary?

    What I have now is that I'm including these in my source dist for
    Linux, but I don't know how to do some type of conditional install. I
    tried adding the Pmw package in my setup.py, but the Pmw.def file
    doesn't make it over. Only the *.py files are in the MANIFEST. I
    suppose I could change my MANIFEST.in file, but it seems to me that
    having "Pmw" in the packages list should be sufficient.

    Does Python2.4 have more extensive distutils documentation? (I'm using
    2.3.2 now.)

    Thanks.
    , Mar 8, 2005
    #7
  8. Frans Englich

    Carl Banks Guest

    Frans Englich wrote:
    > This is silly. How do I access data files I've installed with

    distutils? In a
    > portable, generic way, I want to find out what is the following path

    on most
    > systems:
    >
    > /usr/local/lib/python2.4/lib/site-packages/foo/bar.txt
    >
    > How do I figure out the rest, if I know foo/bar.txt? sys.prefix

    doesn't get me
    > far.
    >
    > I've googled, and looked in the python reference. I must be blind if

    the
    > distutils section[1] covers this.


    It doesn't, because distutils doens't do this. The data_files option
    to distutils is pretty limited, is buggy, and if you override the data
    dir, distutils has absolutely no way to let the Python code know about
    it. Once or twice I've thought about making a patch, but it's a
    difficult problem since different parts of distutils need to
    communicate to make it work, in a way distutils engine doesn't support.

    Workarounds:

    Probably the most straightforward way is to just assume the data files
    are in the default place, but provide a way to override with an
    environment variable. If the user overrides the data dir when
    installing, then it's his or her responsibility to define the
    environment variable declaring where the data is.

    The second workaround is to inspect the distutils structure (that which
    is returned by setup), find out where the data dir is, and create a
    Python module that sets the data file location. I did this for a
    certain package I wrote. I posted this example here in
    comp.lang.python a while back:

    http://tinyurl.com/5qgsw


    --
    CARL BANKS
    Carl Banks, Mar 8, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Michael_Str=F6der?=

    autoconf-style checking for installed libs with DistUtils

    =?ISO-8859-1?Q?Michael_Str=F6der?=, Jul 26, 2003, in forum: Python
    Replies:
    2
    Views:
    303
    Hartmut Goebel
    Jul 28, 2003
  2. Tristan Miller
    Replies:
    1
    Views:
    649
    Tristan Miller
    Jul 23, 2004
  3. timw.google
    Replies:
    1
    Views:
    511
    Serge Orlov
    May 11, 2006
  4. Luis Alberto Zarrabeitia Gomez

    distutils, No module named numpy.distutils.fcompiler.conv_template

    Luis Alberto Zarrabeitia Gomez, Mar 30, 2009, in forum: Python
    Replies:
    0
    Views:
    1,284
    Luis Alberto Zarrabeitia Gomez
    Mar 30, 2009
  5. Philippe Ombredanne
    Replies:
    0
    Views:
    134
    Philippe Ombredanne
    Jan 31, 2013
Loading...

Share This Page