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}
3
+50

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}

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.