PythonTeX:导入/打印文件夹中的所有 Python 文件

PythonTeX:导入/打印文件夹中的所有 Python 文件

我使用 Pythontex 来记录我的讲义。我有很多包含经典脚本(例如随机变量的模拟)的 .py 文件,我在许多文件中使用这些脚本。

因此,我的目标如下:我想自动导入和排版.py 文件。

根据这一命题(https://github.com/gpoore/pythontex/issues/27),我尝试了以下 MWE,其中\importandtypeset定义了一个命令(我假设bernoulli.py在 中absolutepath_onmymac/)。但我得到了以下错误(当我在 之外执行命令时,一切工作\inputpygments正常\pyc{from bernoulli import *}\importandtypeset

"""
Traceback (most recent call last):
  File "/miniconda3/lib/python3.7/multiprocessing/pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/texlive/2019/texmf-dist/scripts/pythontex/pythontex3.py", line 2319, in do_pygments
    processed = highlight(content, lexer[c.family], formatter[c.family])
  File "/miniconda3/lib/python3.7/site-packages/pygments/__init__.py", line 85, in highlight
    return format(lex(code, lexer), formatter, outfile)
  File "/miniconda3/lib/python3.7/site-packages/pygments/__init__.py", line 43, in lex
    return lexer.get_tokens(code)
  File "/miniconda3/lib/python3.7/site-packages/pygments/lexer.py", line 150, in get_tokens
    text, _ = guess_decode(text)
  File "/miniconda3/lib/python3.7/site-packages/pygments/util.py", line 310, in guess_decode
    text = text.decode('utf-8')
AttributeError: 'NoneType' object has no attribute 'decode'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/TeX/texbin/pythontex", line 62, in <module>
    pythontex.main()
  File "/usr/local/texlive/2019/texmf-dist/scripts/pythontex/pythontex3.py", line 2716, in main
    do_multiprocessing(data, temp_data, old_data, engine_dict)
  File "/usr/local/texlive/2019/texmf-dist/scripts/pythontex/pythontex3.py", line 1371, in do_multiprocessing
    result = task.get()
  File "/miniconda3/lib/python3.7/multiprocessing/pool.py", line 657, in get
    raise self._value
AttributeError: 'NoneType' object has no attribute 'decode'

分数维:

\documentclass[a4paper]{book}

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

\usepackage[rerun=always]{pythontex}

\begin{pythontexcustomcode}[begin]{py}
from pylab import *
import random as rd
import sys
import os
sys.path.append('absolutepath_onmymac/')
def addtopath(p):
    if p and p not in sys.path:
        sys.path.append(p)
\end{pythontexcustomcode}

\newcommand{\importandtypeset}[1][.]{%
  \pyc{add_to_path('absolutepath_onmymac/'); from #1 import *; pytex.add_dependencies(os.path.join('absolutepath_onmymac/', '#1.py'))}%
  \inputpygments{python}{absolutepath_onmymac/#1.py}%
}

%%%%%%%%%%%%
\begin{document}

\inputpygments{python}{absolutepath_onmymac/bernoulli.py}
\pyc{from bernoulli import *}
\py{bernoulli}

\importandtypeset{bernoulli}

\end{document}

相关内容