This is about a file location problem while using pythontex (which works great!)

I have some python code within \begin{pylabblock} \end{pylabblock} where I am reading in data from a file in the current directory

code fragment

filename = str(os.getcwd()+'/testdata1.dat')

x = xyread(filename)[0]
y = xyread(filename)[1]

the xyread fails - it cannot find the file in the getcwd()

BUT if I place a copy of the testdata1.dat in a subdirectory that pythontex creates during compile, it works

(my tex file is named test3.tex and pythontex creates pythontex-files-test3/ and if testdata1.dat is in that directory, it works)


By default, the directory that pythontex creates is the working directory. This keeps everything created by pythontex isolated. If you want to use the document directory as the working directory, you can use \setpythontexworkingdir{.} in your preamble, or os.chdir('..') in your Python code.


Starting with version 0.14, pythontex's default working directory is the document directory. This simplifies directory handling. This can still be changed from the TeX side with \setpythontexworkingdir or the Python side with os.chdir().

  • Ah - thanks for the answer ... And oh, very helpful package indeed ... appreciate it, – Krishnan Sep 7 '13 at 21:34
  • Another option is just to use ../filename everywhere, both for reading as well as writing. This will work on Unix-like systems, at least. – Faheem Mitha Mar 10 '15 at 8:31

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.