1

I do not know how to import a function from another file using pycode.

I have a file named csv_code.py with the following code,

import csv
def create_csv():

header = ['name', 'degree'] data = ['Mark', 6]

with open('degrees.csv', 'w', encoding='UTF8', newline='') as f: writer = csv.writer(f) writer.writerow(header) writer.writerow(data)

and in the same directory, I have a .tex file containing this code

\documentclass{article}
\usepackage{csvsimple}
\usepackage{pythontex}

\begin{document}

\begin{pycode}
from csv_code import *
create_csv()
\end{pycode}

\csvautotabular{countries.csv}

\end{document}

It works good, but once it has been compiled if the code in csv_code is changed (let's say that instead of Mark now we use the name John) when compiled again the document doesn't suffer any changes.

I assume LaTeX is just lazy and as it has run that code already it doesn't run it again. Is there a way of forcing LaTeX to run that code every time?

I have read here a solution using --shell-escape. But I don't know how to set this up in VSCode and I would like to find a way that doesn't involve playing with the settings to make it easier if I share my code.

  • 1
    Does it help if you declare your imported file as a dependency explicitly using the pytex.add_dependencies() function of PythonTeX? Like here https://github.com/gpoore/pythontex/issues/27#issuecomment-28412480 –  May 04 '22 at 15:17
  • Not really, but thank you. This has been useful to solve a different issue. – Tereso del Río Almajano May 04 '22 at 15:39

1 Answers1

3

For me, adding pytex.add_dependencies('csv_code.py') works. I think it's not a problem of LaTeX but of PythonTeX. PythonTeX does not look for changes in csv_code.py. I guess PythonTeX generally does not look for changes in imports. Therefore, you have to tell it that the file is a dependency.

The workflow to demonstrate it is as follows:

  1. Clean all auxiliary files that might still be there
  2. Compile document the first time
pdflatex main
python3 $(which pythontex) main
pdflatex main
  1. PDF contains Mark
  2. Change Mark to John in csv_code.py
  3. Compile document the second time
python3 $(which pythontex) main
pdflatex main
  1. PDF contains John

Here are the files. (I changed them a little to make them compilable. I hope I didn't change anything fundamental to your problem):

main.tex:

\documentclass{article}
\usepackage{csvsimple}
\usepackage{pythontex}

\pyc{pytex.add_dependencies('csv_code.py')}

\begin{document}

\begin{pycode} from csv_code import * create_csv() \end{pycode}

\IfFileExists{degrees.csv}{\csvautotabular{degrees.csv}}{}

\end{document}

csv_code.py:

import csv

def create_csv(): header = ['name', 'degree'] data = ['Mark', 6]

with open('degrees.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(header)
    writer.writerow(data)

  • 1
    I wasn't following the steps appropriately, thank you for the detailed explanation! I agree that it seems like the problem is coming from PythonTeX, not LaTeX as I suggested. – Tereso del Río Almajano May 04 '22 at 17:36