12

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.

9

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}
  • Thanks! Is this bug just in pyconsole, or in pycode, etc, also? [It doesn't work in pycode for me.] – Chris Chudzicki Aug 23 '13 at 1:25
  • 1
    @mrc It's a bug in everything console-related. It won't work by default in pycode, because even though the current working directory is added to sys.path, by default the current working directory is always the PythonTeX output directory, not the document root directory. That way, everything generated by PythonTeX is isolated in a single location (if you don't specify paths). If you want the document directory to be the working directory, you can use \setpythontexworkingdir{.}. In the next release, I will probably set things always to add the document directory to sys.path. – G. Poore Aug 23 '13 at 1:43
  • 1
    @mrc I just released PythonTeX v0.12, which incorporates the bugfix. The console commands and environments will now behave like the normal ones, so far as sys.path is concerned. – G. Poore Aug 27 '13 at 4:56

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.