3

I have been trying to get PythonTeX working, but can't seem to solve the errors I'm getting.

My current Minimum Broken Example is this:

% !TEX TS-program = xelatex
\documentclass{article}

\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}

\usepackage[pyfuture=all]{pythontex}

\begin{document}
Hello world!

\begin{pycode}
print "foo"
\end{pycode}

\end{document}

Which, unless I'm misunderstanding the documentation, should output foo into that location in the LaTeX document.

Almost all the other features are working, such as the console emulation mode:

\documentclass{article}

\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}

\usepackage[pyfuture=all]{pythontex}

\begin{document}
Hello world!

\begin{pyconsole}
x = 123
y = 345
z = x + y
z
def f(expr):
    return(expr**4)

f(x)
print('Python says hi from the console!')
\end{pyconsole}

\end{document}

So does anyone have any idea why the pycode environment doesn't work?


Edit: More information:

It seems I confused things, above.

I've found (though it took longer than I'd like to admit) that this does work:

% !TEX TS-program = xelatex
\documentclass{article}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}

\usepackage[]{pythontex}

\begin{document}
\begin{pycode}
print("hello")
\end{pycode}
\end{document}

But regardless of having pyfuture=all selected or not, why cant I use the python 2.7 print "foo" syntax? I get the following error:

This is PythonTeX 0.14

----  Messages for py:default:default  ----
* PythonTeX stderr - error on line 19:
    File "<outputdir>/py_default_default.py", line 54
      print "hello"
                  ^
  SyntaxError: invalid syntax

And one last question, can I pre-import things into the console emulator? Something like this, but this doesn't work:

% !TEX TS-program = xelatex
\documentclass{article}

\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}

\usepackage[]{pythontex}


\begin{document}
foo

\begin{pycode}
import math
\end{pycode}
\begin{pyconsole}
math.sqrt(25)
\end{pyconsole}

\end{document}

Should I just try to switch everything to python 3?

  • If you have pyfuture=all, I think you want print("foo"), with the parentheses for the print() function. Have you tried that? Also, giving the actual error messages would be helpful. – G. Poore Mar 31 '15 at 2:08
  • Thanks so much for replying, I've updated my answer with what I figured out since asking. – Ryan Mar 31 '15 at 2:54
  • It looks like you want pyfuture=none (no auto imports from __future__) and the pyconcode environment. I can add a more detailed answer later. – G. Poore Mar 31 '15 at 3:44
  • is pyfuture=none not the default? I assumed it was... Is there a way to be explicit about what to pull in from __future__? If I know I want division, for example? Thanks for taking the time, and thanks for the awesome package! – Ryan Mar 31 '15 at 5:18
3

Most of the issues are related to Python's __future__ module, which allows Python 3 features to be imported in Python 2. Imports from __future__ can be performed manually. However, by default PythonTeX brings in three things:

  • print_function
  • absolute_import
  • division

This corresponds to the package option pyfuture=default. Basically, the default behavior is to behave as much like Python 3 as possible without a significant chance of conflicts. The only other possible import from __future__ for Python 2.7 is unicode_literals, but when I was creating the package a few years ago, that could cause problems with some packages. Things should be better now, but I expect that an occasional conflict is still possible.

If you want to add unicode_literals, you can use pyfuture=all. If you want no automatic imports from __future__, just use the package option pyfuture=none, and then import from __future__ as you normally would.

Regarding your console question, the pyconcode environment will execute within the console session, but show no output. Everything console-related starts with the prefix pycon. Also note that the console treatment of automatic __future__ imports is governed by the package option pyconfuture.

  • 1
    Makes sense why the console was different from the pycode environment now, thanks! Thanks for explaining and thanks for writing the package. – Ryan Mar 31 '15 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.