python parser overridden by pymol

Discussion in 'Python' started by Jeremiah, Nov 12, 2009.

  1. Jeremiah

    Jeremiah Guest

    Hello,

    I'm fairly new to python (version 2.5.4), and am writing a program
    which uses both pymol (version 1.2r1) and numpy (version 1.3.0) from
    debian.

    It appears that when I add pymol to $PYTHONPATH, that parser.expr() is
    no longer available, and so I am unable to use numpy.load(). I have
    looked for where parser.expr() is defined in the python system so I
    could place that directory first in $PYTHONPATH, but I have not been
    able to find the file that defines expr().

    My reason for using numpy.load() is that I have a numpy array which
    takes an hour to generate. Therefore, I'd like to use numpy.save() so
    I could generate the array one time, and then load it later as needed
    with numpy.load().

    I've successfully tested the use of numpy.save() and numpy.load() with
    a small example when the pymol path is not defined in $PYTHONPATH :

    >>> import numpy
    >>> numpy.save('123',numpy.array([1,2,3]))
    >>> numpy.load('123.npy')

    array([1, 2, 3])


    However, a problem arises once $PYTHONPATH includes the pymol
    directory. To use the pymol api, I add the following to ~/.bashrc:

    PYMOL_PATH=/usr/lib/pymodules/python2.5/pymol
    export PYMOL_PATH
    PYTHONPATH=$PYMOL_PATH
    export PYTHONPATH

    Once this is done, numpy.load() no longer works correctly, as pymol
    contains a file named parser.py ( /usr/lib/pymodules/python2.5/pymol/
    parser.py ), which apparently prevents python from using its native
    parser.

    >>> numpy.load('123.npy')

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/site-packages/numpy/lib/io.py", line
    195, in load
    return format.read_array(fid)
    File "/usr/lib/python2.5/site-packages/numpy/lib/format.py",
    line 353, in read_array
    shape, fortran_order, dtype = read_array_header_1_0(fp)
    File "/usr/lib/python2.5/site-packages/numpy/lib/format.py",
    line 250, in read_array_header_1_0
    d = safe_eval(header)
    File "/usr/lib/python2.5/site-packages/numpy/lib/utils.py", line
    840, in safe_eval
    ast = compiler.parse(source, "eval")
    File "/usr/lib/python2.5/compiler/transformer.py", line 54, in
    parse
    return Transformer().parseexpr(buf)
    File "/usr/lib/python2.5/compiler/transformer.py", line 133, in
    parseexpr
    return self.transform(parser.expr(text))
    AttributeError: 'module' object has no attribute 'expr'

    If I understand the problem correctly, can anyone tell me where
    python.expr() is defined, or suggest a better method to fix this
    problem?

    Thanks,
    Jeremiah
    Jeremiah, Nov 12, 2009
    #1
    1. Advertising

  2. Jeremiah

    Robert Kern Guest

    Jeremiah wrote:

    > However, a problem arises once $PYTHONPATH includes the pymol
    > directory. To use the pymol api, I add the following to ~/.bashrc:
    >
    > PYMOL_PATH=/usr/lib/pymodules/python2.5/pymol
    > export PYMOL_PATH
    > PYTHONPATH=$PYMOL_PATH
    > export PYTHONPATH


    Don't change your PYTHONPATH like that. You want to put
    /usr/lib/pymodules/python2.5 onto your PYTHONPATH and import PyMOL's stuff from
    the pymol package. I.e., instead of

    import api

    Do

    from pymol import api

    pymol is a package for precisely this reason.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Nov 12, 2009
    #2
    1. Advertising

  3. On Wed, 11 Nov 2009 17:41:07 -0800, Jeremiah wrote:

    > Hello,
    >
    > I'm fairly new to python (version 2.5.4), and am writing a program which
    > uses both pymol (version 1.2r1) and numpy (version 1.3.0) from debian.
    >
    > It appears that when I add pymol to $PYTHONPATH, that parser.expr() is
    > no longer available, and so I am unable to use numpy.load(). I have
    > looked for where parser.expr() is defined in the python system so I
    > could place that directory first in $PYTHONPATH, but I have not been
    > able to find the file that defines expr().



    >>> import parser
    >>> parser.__file__

    '/usr/lib/python2.5/lib-dynload/parsermodule.so'
    >>> parser.expr

    <built-in function expr>



    [...]
    > However, a problem arises once $PYTHONPATH includes the pymol
    > directory. To use the pymol api, I add the following to ~/.bashrc:
    >
    > PYMOL_PATH=/usr/lib/pymodules/python2.5/pymol
    > export PYMOL_PATH
    > PYTHONPATH=$PYMOL_PATH
    > export PYTHONPATH



    Change that to

    PYMOL_PATH=/usr/lib/pymodules/python2.5


    and it should work, assuming pymol uses a package, as it should. If it
    doesn't, if it's just a hodge-podge of loose modules in a directory, then
    they should be slapped with a wet fish for shadowing a standard library
    module.



    --
    Steven
    Steven D'Aprano, Nov 12, 2009
    #3
  4. Jeremiah

    Dave Angel Guest

    Jeremiah wrote:
    > Hello,
    >
    > I'm fairly new to python (version 2.5.4), and am writing a program
    > which uses both pymol (version 1.2r1) and numpy (version 1.3.0) from
    > debian.
    >
    > It appears that when I add pymol to $PYTHONPATH, that parser.expr() is
    > no longer available, and so I am unable to use numpy.load(). I have
    > looked for where parser.expr() is defined in the python system so I
    > could place that directory first in $PYTHONPATH, but I have not been
    > able to find the file that defines expr().
    >
    > My reason for using numpy.load() is that I have a numpy array which
    > takes an hour to generate. Therefore, I'd like to use numpy.save() so
    > I could generate the array one time, and then load it later as needed
    > with numpy.load().
    >
    > I've successfully tested the use of numpy.save() and numpy.load() with
    > a small example when the pymol path is not defined in $PYTHONPATH :
    >
    > >>> import numpy
    > >>> numpy.save('123',numpy.array([1,2,3]))
    > >>> numpy.load('123.npy')

    > array([1, 2, 3])
    >
    >
    > However, a problem arises once $PYTHONPATH includes the pymol
    > directory. To use the pymol api, I add the following to ~/.bashrc:
    >
    > PYMOL_PATH=/usr/lib/pymodules/python2.5/pymol
    > export PYMOL_PATH
    > PYTHONPATH=$PYMOL_PATH
    > export PYTHONPATH
    >
    > Once this is done, numpy.load() no longer works correctly, as pymol
    > contains a file named parser.py ( /usr/lib/pymodules/python2.5/pymol/
    > parser.py ), which apparently prevents python from using its native
    > parser.
    >
    > >>> numpy.load('123.npy')

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "/usr/lib/python2.5/site-packages/numpy/lib/io.py", line
    > 195, in load
    > return format.read_array(fid)
    > File "/usr/lib/python2.5/site-packages/numpy/lib/format.py",
    > line 353, in read_array
    > shape, fortran_order, dtype = read_array_header_1_0(fp)
    > File "/usr/lib/python2.5/site-packages/numpy/lib/format.py",
    > line 250, in read_array_header_1_0
    > d = safe_eval(header)
    > File "/usr/lib/python2.5/site-packages/numpy/lib/utils.py", line
    > 840, in safe_eval
    > ast = compiler.parse(source, "eval")
    > File "/usr/lib/python2.5/compiler/transformer.py", line 54, in
    > parse
    > return Transformer().parseexpr(buf)
    > File "/usr/lib/python2.5/compiler/transformer.py", line 133, in
    > parseexpr
    > return self.transform(parser.expr(text))
    > AttributeError: 'module' object has no attribute 'expr'
    >
    > If I understand the problem correctly, can anyone tell me where
    > python.expr() is defined, or suggest a better method to fix this
    > problem?
    >
    > Thanks,
    > Jeremiah
    >
    >

    Generic answers, I have no experience with pymol

    If pymol really needs that parser.py, you have a problem, as there can
    only be one module by that name in the application. But assuming it's
    needed for some obscure feature that you don't need, you could try the
    following sequence.

    1) temporarily rename the pymol's parser.py file to something else,
    like pymolparser.py, and see what runs.
    2) rather than changing the PYTHONPATH, fix up sys.path during your
    script initialization.
    In particular, do an import parser near the beginning of the
    script. This gets it loaded, even though you might not need to use it
    from this module.
    After that import, then add the following line (which could be
    generalized later)
    sys.path.append( "/usr/lib/pymodules/python2.5/pymol")


    If this works, then you can experiment a bit more, perhaps you don't
    need the extra import parser, just putting the pymol directory at the
    end of the sys.path rather than the beginning may be good enough.

    If the parser.py in the pymol is actually needed, you might need to
    rename its internal references to some other name, like pymolparser.

    HTH,
    DaveA
    Dave Angel, Nov 12, 2009
    #4
  5. On Wed, Nov 11, 2009 at 7:48 PM, Dave Angel <> wrote:
    >
    >
    > Jeremiah wrote:
    >>
    >> Hello,
    >>
    >> I'm fairly new to python (version 2.5.4), and am writing a program
    >> which uses both pymol (version 1.2r1) and numpy (version 1.3.0) from
    >> debian.
    >>
    >> It appears that when I add pymol to $PYTHONPATH, that parser.expr() is
    >> no longer available, and so I am unable to use numpy.load(). I have
    >> looked for where parser.expr() is defined in the python system so I
    >> could place that directory first in $PYTHONPATH, but I have not been
    >> able to find the file that defines expr().
    >>
    >> My reason for using numpy.load() is that I have a numpy array which
    >> takes an hour to generate. Therefore, I'd like to use numpy.save() so
    >> I could generate the array one time, and then load it later as needed
    >> with numpy.load().
    >>
    >> I've successfully tested the use of numpy.save() and numpy.load() with
    >> a small example when the pymol path is not defined in $PYTHONPATH  :
    >>
    >>   >>> import numpy
    >>   >>> numpy.save('123',numpy.array([1,2,3]))
    >>   >>> numpy.load('123.npy')
    >>   array([1, 2, 3])
    >>
    >>
    >> However, a problem arises once $PYTHONPATH includes the pymol
    >> directory. To use the pymol api, I add the following to ~/.bashrc:
    >>
    >>   PYMOL_PATH=/usr/lib/pymodules/python2.5/pymol
    >>   export PYMOL_PATH
    >>   PYTHONPATH=$PYMOL_PATH
    >>   export PYTHONPATH
    >>
    >> Once this is done, numpy.load() no longer works correctly, as pymol
    >> contains a file named parser.py ( /usr/lib/pymodules/python2.5/pymol/
    >> parser.py ), which apparently prevents python from using its native
    >> parser.
    >>
    >>   >>> numpy.load('123.npy')
    >>   Traceback (most recent call last):
    >>     File "<stdin>", line 1, in <module>
    >>     File "/usr/lib/python2.5/site-packages/numpy/lib/io.py", line
    >> 195, in load
    >>       return format.read_array(fid)
    >>     File "/usr/lib/python2.5/site-packages/numpy/lib/format.py",
    >> line 353, in read_array
    >>       shape, fortran_order, dtype = read_array_header_1_0(fp)
    >>     File "/usr/lib/python2.5/site-packages/numpy/lib/format.py",
    >> line 250, in read_array_header_1_0
    >>       d = safe_eval(header)Thank you. That really helped.


    To use pymol and numpy to
    >>     File "/usr/lib/python2.5/site-packages/numpy/lib/utils.py", line
    >> 840, in safe_eval
    >>       ast = compiler.parse(source, "eval")
    >>     File "/usr/lib/python2.5/compiler/transformer.py", line 54, in
    >> parse
    >>       return Transformer().parseexpr(buf)
    >>     File "/usr/lib/python2.5/compiler/transformer.py", line 133, in
    >> parseexpr
    >>       return self.transform(parser.expr(text))
    >>   AttributeError: 'module' object has no attribute 'expr'
    >>
    >> If I understand the problem correctly, can anyone tell me where
    >> python.expr() is defined, or suggest a better method to fix this
    >> problem?
    >>
    >> Thanks,
    >> Jeremiah
    >>
    >>

    >
    > Generic answers, I have no experience with pymol
    >
    > If pymol really needs that parser.py, you have a problem, as there can only
    > be one module by that name in the application.  But assuming it's needed for
    > some obscure feature that you don't need, you could try the following
    > sequence.
    >
    > 1) temporarily rename the pymol's  parser.py  file to something else, like
    > pymolparser.py, and see what runs.
    > 2) rather than changing the PYTHONPATH, fix  up  sys.path during your script
    > initialization.
    >   In particular, do an    import parser    near the beginning of the script.
    >  This gets it loaded, even though you might not need to use it from this
    > module.
    >   After that import, then add the following line (which could be generalized
    > later)
    >   sys.path.append( "/usr/lib/pymodules/python2.5/pymol")
    >
    >
    > If this works, then you can experiment a bit more, perhaps you don't need
    > the extra import parser, just putting the pymol directory at the end of the
    > sys.path rather than the beginning may be good enough.
    >
    > If the parser.py in the pymol is actually needed, you might need to rename
    > its internal references to some other name, like pymolparser.
    >
    > HTH,
    > DaveA
    >
    >


    Thank you. Your second suggestion really helped.

    To use pymol and numpy together, I now do the following:

    To ~/.bashrc add:
    PYMOL_PATH=/usr/lib/pymodules/python2.5/pymol
    export PYMOL_PATH

    Then I can do the following in python:

    import numpy
    numpy.save('123',numpy.array([1,2,3]))
    numpy.load('123.npy')
    array([1, 2, 3])
    import sys
    sys.path.append( "/usr/lib/pymodules/python2.5/pymol")
    import pymol
    pymol.finish_launching()
    pymol.importing.load("/path/to/file.pdb")

    Thanks,
    Jeremiah
    Jeremiah H. Savage, Nov 13, 2009
    #5
  6. Jeremiah

    Robert Kern Guest

    Jeremiah H. Savage wrote:

    > To use pymol and numpy together, I now do the following:
    >
    > To ~/.bashrc add:
    > PYMOL_PATH=/usr/lib/pymodules/python2.5/pymol
    > export PYMOL_PATH
    >
    > Then I can do the following in python:
    >
    > import numpy
    > numpy.save('123',numpy.array([1,2,3]))
    > numpy.load('123.npy')
    > array([1, 2, 3])
    > import sys
    > sys.path.append( "/usr/lib/pymodules/python2.5/pymol")
    > import pymol
    > pymol.finish_launching()
    > pymol.importing.load("/path/to/file.pdb")


    No, do not do this. Add /usr/lib/pymodules/python2.5/ to your $PYTHONPATH, *not*
    /usr/lib/pymodules/python2.5/pymol/. You will continue to run into problems if
    you do it this way. You are not supposed to put the directory *of* the package
    onto sys.path but rather the directory that *contains* the package directory.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Nov 13, 2009
    #6
  7. Jeremiah

    Dave Angel Guest

    Robert Kern wrote:
    > <div class="moz-text-flowed" style="font-family: -moz-fixed">Jeremiah
    > H. Savage wrote:
    >
    >> To use pymol and numpy together, I now do the following:
    >>
    >> To ~/.bashrc add:
    >> PYMOL_PATH=/usr/lib/pymodules/python2.5/pymol
    >> export PYMOL_PATH
    >>
    >> Then I can do the following in python:
    >>
    >> import numpy
    >> numpy.save('123',numpy.array([1,2,3]))
    >> numpy.load('123.npy')
    >> array([1, 2, 3])
    >> import sys
    >> sys.path.append( "/usr/lib/pymodules/python2.5/pymol")
    >> import pymol
    >> pymol.finish_launching()
    >> pymol.importing.load("/path/to/file.pdb")

    >
    > No, do not do this. Add /usr/lib/pymodules/python2.5/ to your
    > $PYTHONPATH, *not* /usr/lib/pymodules/python2.5/pymol/. You will
    > continue to run into problems if you do it this way. You are not
    > supposed to put the directory *of* the package onto sys.path but
    > rather the directory that *contains* the package directory.
    >

    As I said before, I don't know pymol. But if that is the package name,
    then Robert is certainly right. You need to read the docs on pymol to
    see what they require. For example, it's surprising they require a
    separate PYMOL_PATH environment variable, since they can find their own
    directory path with the __file__ attribute of one of the modules.

    Anyway, one more generic comment. Rather than having that directory in
    both the bashrc file AND in your python source, I'd consider deriving
    the latter from the environment variable, once you determine that it's
    actually necessary. And of course you could strip the last node from
    the path in the environment variable before appending it to sys.path, if
    that's what's appropriate.

    DaveA
    Dave Angel, Nov 13, 2009
    #7
    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. Laurence Neville

    Datagrid CssClass being overridden by TD style

    Laurence Neville, Sep 22, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    5,086
    Laurence Neville
    Sep 23, 2004
  2. Patel
    Replies:
    6
    Views:
    40,478
    Randall R Schulz
    Oct 23, 2003
  3. Patel
    Replies:
    1
    Views:
    402
    Harald Hein
    Oct 23, 2003
  4. Marco
    Replies:
    7
    Views:
    9,150
    Alex Hunsley
    Sep 13, 2004
  5. Xiao Jianfeng
    Replies:
    2
    Views:
    758
    Ben Finney
    Dec 16, 2005
Loading...

Share This Page