Attribute error using python scientific module on Windows
I get the following error when our program is run on Windows XP using ActivePerl 2.6 (although it runs fine on linux). Any ideas of what is going on? Is this a bug in the module or a problem with our code?
Error log:
File "C:\Python26\lib\site-packages\Scientific\IO\PDB.py", line 1163, in __ini
t__
self.parseFile(PDBFile(file_or_filename))
File "C:\Python26\lib\site-packages\Scientific\IO\PDB.py", line 1371, in parse
File
type, data = file.readLine()
File "C:\Python26\lib\site-packages\Scientific\IO\PDB.py", line 200, in readLi
ne
line = self.file.readline()
AttributeError: 'unicode' object has no attribute 'readline'
Exception AttributeError: "'unicode' object has n开发者_StackOverflow社区o attribute 'close'" in <bound
method PDBFile.__del__ of <Scientific.IO.PDB.PDBFile instance at 0x00FD0440>> ig
nored
UPDATE: Following Joe's suggestion below, I get the following error instead:
Traceback (most recent call last):
File "NewParseV1_00.py", line 47, in <module>
PromptUser()
File "NewParseV1_00.py", line 45, in PromptUser
parseGroupFile(grpfilepath, outputPPYfilepath, sorcePDBfilepath)
File "NewParseV1_00.py", line 39, in parseGroupFile
return GeneratePPY(LL,GRPNAME,SRCPDB,OUTPPY)
File "NewParseV1_00.py", line 10, in __init__
self.PDBStruct = Scientific.IO.PDB.Structure(SRCPDB)
File "C:\Python26\lib\site-packages\Scientific\IO\PDB.py", line 1163, in __ini
t__
self.parseFile(PDBFile(file_or_filename))
File "C:\Python26\lib\site-packages\Scientific\IO\PDB.py", line 161, in __init
__
if isinstance(file_or_filename, basestr):
NameError: global name 'basestr' is not defined
Exception AttributeError: "PDBFile instance has no attribute 'open'" in <bound m
ethod PDBFile.__del__ of <Scientific.IO.PDB.PDBFile instance at 0x00FDB418>> ign
ored
I'm guessing, but it looks like a bug in Scientific.IO.PDB
where they do something like:
if type(file_or_filename) == str:
file = open(file_or_filename)
else:
file = file_or_filename
rather than doing:
if isinstance(file_or_filename, basestr):
...
For whatever reason, you appear to be passing in a unicode filename on the windows side, and a raw string on the linux side.
Again, though, I'm just taking a guess. I'm assuming Scientific
here is this module? http://dirac.cnrs-orleans.fr/plone/software/scientificpython/ (I'm not familiar with it...)
Edit: Yep!
Here's the relevant code from Scientific.IO.PDB
:
class PDBFile:
"""
X{PDB} file with access at the record level
The low-level file access is handled by the module
L{Scientific.IO.TextFile}, therefore compressed files and URLs
(for reading) can be used as well.
"""
def __init__(self, file_or_filename, mode = 'r', subformat = None):
"""
@param file_or_filename: the name of the PDB file, or a file object
@type file_or_filename: C{str} or C{file}
@param mode: the file access mode, 'r' (read) or 'w' (write)
@type mode: C{str}
@param subformat: indicates a specific dialect of the PDB format.
Subformats are defined in
L{Scientific.IO.PDBExportFilters}; they are used
only when writing.
@type subformat: C{str} or C{NoneType}
"""
if isinstance(file_or_filename, str):
self.file = TextFile(file_or_filename, mode)
else:
self.file = file_or_filename
<snip>
It should be as simple as changing isinstance(file_or_filename, str)
to isinstance(file_or_filename, basestr)
. You might want to file a bug report...
Changing the code in Scientific.IO.PDB as explained by Joe needs one final modification to work:
On line 161, change if isinstance(file_or_filename, str):
to if isinstance(file_or_filename, basestring):
(Note that Joe wrote that the second argument should be basestr
but that gives the error in the update in the original question.)
精彩评论