[Tex/LaTex] PythonTeX: importing python files

importpython

I'm trying to import a personal python module using python tex, but pythontex doesn't find the module. My attempt:

The LaTeX File:

% !TEX TS-program = pythontex
\documentclass[11pt]{article}
\usepackage[makestderr]{pythontex}

\begin{document}

\begin{pyconsole}
import os
os.getcwd()
import sayhi
\end{pyconsole}

\end{document}

The Python module sayhi.py, in the same directory (/Users/christopherchudzicki/Desktop/pythontex/importproblem) as the LaTeX file:

def hi():
    print("hi")

LaTeX Output:
LaTeX Output

I'd like to be able to call sayhi.hi() from within the pyconsole environment.

Best Answer

This is a bug in the console environments. The current working directory isn't being added to sys.path. I will fix this in the next release.

If you're using PythonTeX v0.12beta (on GitHub) you can use the following workaround for now.

\documentclass[11pt]{article}

\usepackage[makestderr]{pythontex}

\begin{document}

\begin{pyconcode}
import os
import sys
sys.path.append(os.getcwd())
\end{pyconcode}

\begin{pyconsole}
import sayhi
sayhi.hi()
\end{pyconsole}

\end{document}