[reportlab-users] Run time error with version 3.4

751 views
Skip to first unread message

lizhe Xu via reportlab-users

unread,
May 18, 2017, 9:39:58 AM5/18/17
to reportl...@lists2.reportlab.com, lizhe Xu
My colleague sent me a python script which he is using frequently and no problem or error. I don't know which version of reportlab he uses but it's not the latest verison 3.4. When I got it and run on my Mac with Python 2.7 and the latest version of Reportlab 3.4, I got the following error: 


Traceback (most recent call last):   
File "/Volumes/Data/Python_Analysis/VIralDirectBlast4newModified.py", line 815, in <module> 
  IterateVirusFile(FileID, "/Volumes/Data/Python_Analysis/IBRS2201702/ViralCompleteGenome60559_160818.fasta")   
File "/Volumes/Data/Python_Analysis/VIralDirectBlast4newModified.py", line 304, in IterateVirusFile         
  BlastSequencesV29("/Volumes/Data/Python_Analysis/VirusQue.fasta", "blastn", ViralQueID, FileID)   
File "/Volumes/Data/Python_Analysis/VIralDirectBlast4newModified.py", line 474, in BlastSequencesV29     
   draw_genome(FileID)   
File "/Volumes/Data/Python_Analysis/VIralDirectBlast4newModified.py", line 546, in draw_genome
   c.save()   
File "/usr/local/lib/python2.7/site-packages/reportlab/pdfgen/canvas.py", line 1237, in save
   self._doc.SaveToFile(self._filename, self)   
File "/usr/local/lib/python2.7/site-packages/reportlab/pdfbase/pdfdoc.py", line 211, in SaveToFile
   raise RunTimeError("class %s instances can only be saved once" % self.__class__.__name__) 
NameError: global name 'RunTimeError' is not defined 
[Finished in 132.485s


Please help to fix the problem. Thanks.



Lz

Peter Cock via reportlab-users

unread,
May 18, 2017, 9:44:49 AM5/18/17
to lizhe Xu, reportlab-users, Peter Cock
That in itself is a typo, RunTimeError should be RuntimeError
which is a built in exception in Python (lower case t).

However, that won't solve the underlying problem which is something
about class instances which can only be solved once?

Peter
> _______________________________________________
> reportlab-users mailing list
> reportl...@lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>
_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
https://pairlist2.pair.net/mailman/listinfo/reportlab-users

Robin Becker

unread,
May 18, 2017, 10:36:58 AM5/18/17
to reportlab-users, lizhe Xu
The typo was fixed in rev 4353. You can get the latest packages from

https://www.reportlab.com/pypi

use pip install -ihttps://www.reportlab.com/pypi -U reportlab

(needs a user id and login). Alternatively just download the latest source from
bitbucket.org.

The content of the error message should give you a hint as to what is going
wrong. However, it is most likely you are trying to save a canvas more than once.


On 18/05/2017 14:44, Peter Cock via reportlab-users wrote:
> That in itself is a typo, RunTimeError should be RuntimeError
> which is a built in exception in Python (lower case t).
>
> However, that won't solve the underlying problem which is something
> about class instances which can only be solved once?
>
> Peter
>
> On Thu, May 18, 2017 at 2:35 PM, lizhe Xu via reportlab-users
> <reportl...@lists2.reportlab.com> wrote:
>> My colleague sent me a python script which he is using frequently and no
>> problem or error. I don't know which version of reportlab he uses but it's
>> not the latest verison 3.4. When I got it and run on my Mac with Python 2.7
...........
>> File "/usr/local/lib/python2.7/site-packages/reportlab/pdfbase/pdfdoc.py",
>> line 211, in SaveToFile
>> raise RunTimeError("class %s instances can only be saved once" %
>> self.__class__.__name__)
>> NameError: global name 'RunTimeError' is not defined
>> [Finished in 132.485s
>>
>>
>> Please help to fix the problem. Thanks.
>>
>>
>>
>> Lz
>>
>> _______________________________________________
>> reportlab-users mailing list
>> reportl...@lists2.reportlab.com
>> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>>
> _______________________________________________
> reportlab-users mailing list
> reportl...@lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>


--
Robin Becker

Robin Becker

unread,
May 19, 2017, 4:51:35 AM5/19/17
to lizhe Xu, reportlab-users
Hi,

I don't know where the idea arose that you can repeatedly save a canvas object,
but more than one person has tried it see

https://bitbucket.org/rptlab/reportlab/issues/107/runtimeerror-typo-in-pdfdocument



The Canvas contains many structured objects including a PDFDocument instance.
That is the class that causes your error. When you save the Canvas lots of
structures get changed. That means the Canvas is no longer valid and should be
thrown away.

To save individual pages the showPage method should be called.

So like

canv = Canvas(...)
for info in mystuff:
make_a_page(info,canv)
canv.save()

should really be

canv = Canvas(...)
for info in mystuff:
make_a_page(info,canv)
canv.showPage()
canv.save()


this may or may not suit if the number of pages is high, but we don't have a
method which incrementally writes the document out. It might have worked in the
past if you were lucky, but it was never intended to be so.

On 18/05/2017 20:30, lizhe Xu wrote:
> I called my colleague who developed the Python code and he is using the script daily without any error. The only difference between his and mine is I'm using the newer vision of reportLab.
> Yes, the script tries to save a canvas for each loop by adding a new page. Attached two files are the results of running the script from different data sets: WithSave.pdf was generated by my colleague with the original script and old version of reportLab; WithoutSave.pdf was generated from my computer by commenting out the line 546 c.save().
> So, I guess the implementation of the new SaveToFile may differ in the different versions. But my goal is to run the script on my computer without bothering my colleague by emailing data to him anymore. So I wonder if there is a method other than save() will do the magic for me.
> Thanks.
>
.........

lizhe Xu via reportlab-users

unread,
May 24, 2017, 8:45:45 AM5/24/17
to Robin Becker, reportlab-users, lizhe Xu
Hi Robin,

Sorry for the late response, I was out of office since last Friday. The script was developed by my colleague and I'm the user. I plan to learn Python but still has not got time to do so.

How to find the version of reportLab installed on a Mac? I wonder if I can uninstall the version 3.4 from my computer, and re-install the same version as my colleague to make it work.

BTW, I changed the statement which caused the runtime error from c.save() to c.showPage(). As a result, the script was completed and the pdf generated is a several-page file, but totally empty without anything drawed. I have to say that I don't quite understand Python, and the script. It may need to modify other statements as well to make it work correctly. 

Thank you very much.


Lizhe



From: Robin Becker <ro...@reportlab.com>
To: lizhe Xu <xulz...@yahoo.com>; reportlab-users <reportl...@lists2.reportlab.com>
Sent: Friday, May 19, 2017 4:51 AM
Subject: Re: [reportlab-users] Run time error with version 3.4
Reply all
Reply to author
Forward
0 new messages