5

How can I make imports in pythontex which apply to all sessions? Consider for example the following pseudocode. I want to have separate sessions sessionA and sessionB, but want to import in both sessions the numpy library. How can I globally do this such that the import statement applies to all sessions?

\documentclass{article}

\usepackage[gobble=auto]{pythontex}

\begin{document}

\begin{pycode}[ApplyToAllSessions]
  from numpy import *
\end{pycode}

\begin{document}

\begin{pycode}[sessionA]
  #from numpy import *
  a = 5
\end{pycode}


\begin{pycode}[sessionB]
  #from numpy import *
  a = 5
\end{pycode}


\end{document}
student
  • 29,003

1 Answers1

4

You can add common code with the command \pythontexcustomc or the pythontexcustomcode environment:

\documentclass{article}

\usepackage[gobble=auto]{pythontex}

\begin{document}

\pythontexcustomc[begin]{py}{
  from numpy import *
}

\begin{pycode}[sessionA]
  #from numpy import *
  a = 5
\end{pycode}


\begin{pycode}[sessionB]
  #from numpy import *
  a = 5
\end{pycode}


\end{document}
student
  • 29,003
Andrew Swann
  • 95,762