sergiocorreia / panflute

An Pythonic alternative to John MacFarlane's pandocfilters, with extra helper functions
http://scorreia.com/software/panflute/
BSD 3-Clause "New" or "Revised" License
500 stars 59 forks source link

How to run with python 3? #110

Closed hoijui closed 4 years ago

hoijui commented 5 years ago

When writing my filter as simple python file, pandoc starts it with "python" (-> python2), because of the ".py" file ending. how can I make it run under python 3? due to my understanding of the related pandoc source code, I should be able to use a file suffix unknown to pandoc (like "*.py3"), make the file executable and adding a shebang for python3. I tried that, but it fails with:

Error running filter add_local_link_prefix.py3: Error in $: endOfInput

with a filter that used to work under python 2.

hoijui commented 5 years ago

I am running it like this (omitted uninteresting switches):

pandoc --filter "add_local_link_prefix.py3"
sergiocorreia commented 4 years ago

That's a good question but I don't even have py2 installed so I'm not able to test for it; will close it down as py2 is no longer supported but feel free to submit a PR if you find a way to force pandoc/panflute to use py3.

Lesik commented 4 years ago

I have been in the same situation as @hoijui and stumbled upon this issue. My python links to python2 (as it is with many Linux distributions) and changing the link to python3 will break many applications (including apt!). So that was out of the question.

The source code linked above says that for files ending in .py it will run python (so: python2). However, when a file ending is not part of the list (not one of .py, .rb, etc.), pandoc will try to run it directly (think of it like ./myfilter instead of python myfilter). So that way the shebang is respected. (@hoijui, you already know this, just explaining the source code out loud for anybody else who stumbles upon this).

@hoijui, you say you've already tried this, but for me this worked:

#!/usr/bin/env python3

import sys
print(sys.version_info)

Save it without .py extension and run with pandoc --filter myfilter.notpyextension. The output should have a major=3 somewhere in it.

hoijui commented 4 years ago

hey @Lesik ! strange.. it does not work for me. using exactly your code and filename, I get:

Error running filter myfilter.notpyextension:\ Error in $: Failed reading: not a valid json value

.. but good it works for you! :-)

Lesik commented 4 years ago

Hi @hoijui,

that error is to be expected, since my script doesn't output a real pandoc filter but just the python version info. But for me, after the error message, it prints the output of my filter (for debug reasons):

Error running filter myfilter.notpyextension:
Error in $: Failed reading: not a valid json value at 'sys.version_info(major=3,minor=7,micro=3,releaselevel='final',serial=0)'

I'm running the latest pandoc (2.9.2.1) though. Maybe your version of pandoc didn't yet have the feature of printing the output of the filter. In that case, try printing to stderr:

#!/usr/bin/env python3

import sys
sys.stderr.write(str(sys.version_info))

In my version of pandoc, this is the output:

sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)Error running filter testfilter.notpyextension:
Error in $: not enough input

(This seems like an issue with pandoc, not panflute.)

hoijui commented 4 years ago

ahh yep that works! it indeed shows major=3.

pandoc 2.7.3

soo.. what do you think.. what should we do for others to not run into this as well? should we open a stackoverflow question? I could ask it, you answer? or feel free to do both of those, if you want.