text2qti icon indicating copy to clipboard operation
text2qti copied to clipboard

: Can't generate .pdf versions of quizzes: UnboundLocalError: local variable 'proc' referenced before assignment

Open mikegilchrist opened this issue 2 years ago • 3 comments

I'm using text2qti 0.6.0.dev5 which I installed from github. I can create the QTI .zip files from my .md files, but when trying to create .pdf versions of my quizzes, I get the following error

$ text2qti --solutions test.pdf test.md
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.7/site-packages/text2qti-0.6.0.dev5-py3.7.egg/text2qti/cmdline.py", line 144, in main
  File "/home/user/anaconda3/lib/python3.7/subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['pandoc', '-f', 'markdown', '-o', '/tmp/test.pdf']' returned non-zero exit status 43.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/bin/text2qti", line 11, in <module>
    load_entry_point('text2qti==0.6.0.dev5', 'console_scripts', 'text2qti')()
  File "/home/user/.local/lib/python3.7/site-packages/text2qti-0.6.0.dev5-py3.7.egg/text2qti/cmdline.py", line 147, in main
UnboundLocalError: local variable 'proc' referenced before assignment

I get the same behavior if I use the --only-solutions option.

I've looked at the code in text2qti/cmdline.py, but can't see why it's a problem. This seems to be the relevant code

                if solutions_path.suffix.lower() == '.pdf':
                    if not shutil.which('pandoc'):
                        raise Text2qtiError('Exporting solutions in PDF format requires Pandoc (https://pandoc.org/)')
                    if not shutil.which('pdflatex'):
                        raise Text2qtiError('Exporting solutions in PDF format requires LaTeX (https://www.tug.org/texlive/ or https://miktex.org/)')
                    if platform.system() == 'Windows':
                        cmd = [shutil.which('pandoc'), '-f', 'markdown', '-o', str(solutions_path)]
                    else:
                        cmd = ['pandoc', '-f', 'markdown', '-o', str(solutions_path)]
                    try:
                        proc = subprocess.run(
                            cmd,
                            input=solutions_text,
                            capture_output=True,
                            check=True,
                            encoding='utf8'
                        )
                    except subprocess.CalledProcessError:
                        raise Text2qtiError(f'Pandoc failed:\n{"-"*78}\n{proc.stderr}\n{"-"*78}')

Here's my test file

% For use with text2qti
Quiz title: Test file
Quiz description: Simple test file for text2qti


% Khan Academy

Title: Key Components of TF Regulation
Points: 8
1. Describe and differentiate between they key components of TF regulation systems: Activators, Repressors, Silencers, and Enhancers.
   Be sure to indicate which are made of DNA and which are made of protein.
... TF are made of protein while elements are made of DNA.
    Activators are TF that bind enhancer elements to promote the transcription of a gene.
    In contrast, repressors are TF that bind to silencer elements which prevent gene transcription.
___

I apologize if this is really a user error, but do appreciate the package. It's very helpful

mikegilchrist avatar Nov 05 '21 16:11 mikegilchrist

There's a bug in the error handling code that is invoked when Pandoc fails. Currently, the error message is using proc.stderr, but there are failure modes where proc never gets defined. I'll have to reorganize this a little.

In terms of why Pandoc is failing, my guess would be that there are missing LaTeX packages. text2qti needs everything that Pandoc usually requires, plus fontawesome. Until the bug is fixed, you might try creating solutions in md format, and then try to convert that to PDF to see if Pandoc will provide additional details.

gpoore avatar Nov 05 '21 21:11 gpoore

Thanks for the work around. It takes care of my issue.

mikegilchrist avatar Nov 06 '21 14:11 mikegilchrist

Update: Problem disappears if you use a more recent version of pandoc, such as 2.16.2

mikegilchrist avatar Dec 07 '21 13:12 mikegilchrist