[Tex/LaTex] Getting PythonTex to work with TexStudio

pythontextexstudio

I cannot get PythonTex to fully work with TexStudio. I'm having a nightmare with this and solved many issues, including activating the conda base, mkl-service library clashes, and TexStudio command paths. My environment variables also needed to be fixed to include the Anaconda library binaries in the search path. The remaining issue I cannot solve after two days is generating plots through PythonTex. The error appears as:
"This application failed to start because it could not find or load Qt platform plugin"windows".
A native Windows cmd has the same problem so I do not think PythonTex is at fault. I think the problem is with importing libraries outside of the python core into TexStudio.

Here is a MWE

\documentclass{article}
\usepackage{pythontex}
\begin{document}
\begin{pycode}
import sys
print(sys.version)
\end{pycode}
\end{document}

This prints: 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]

Here is a nonworking example

\documentclass{article}
\usepackage{pythontex}
\begin{document}
\begin{pylabblock}
rc('text', usetex=True)
rc('font', family='serif')
rc('font', size=10.0)
rc('legend', fontsize=10.0)
rc('font', weight='normal')
x = linspace(0, 10)
figure(figsize=(4, 2.5))
plot(x, sin(x), label='$\sin(x)$')
xlabel(r'$x\mathrm{-axis}$')
ylabel(r'$y\mathrm{-axis}$')
legend(loc='lower right')
savefig('myplot.pdf', bbox_inches='tight')
\end{pylabblock}
\end{document}

I have done the following to try and rectify the problem:

  • Activated conda base
  • Did conda install mkl-service
  • Tried piping conda activation as per this question
  • Verified that there is no clash of MKL libraries using where libiomp5md.dll as indicated here and here
  • Verified the python code in Spyder and in the native cmd prompt
  • Tried adding the QT_PLUGIN_PATH environment variable

Unfortunately, the solution here has not worked for me either. Any help much appreciated.

System:

  • Windows10
  • TexStudio 2.12.6
  • Conda 4.8.2
  • PythonTex 0.17

Best Answer

After trying lots of things, including the suggestions from here, I have found just one solution to solving the Qt issue: start TexStudio from the Anaconda prompt

Even though the environment variables are the same (can check using os.environ in python and easily print output to LaTeX file using the pyconsole environment), this seems to work. Presumably there is a different 'priority' given to paths in the environment?

EDIT

I dug into this a bit further to try and understand why this works. Using !set from a python prompt reveals the environment variables currently active. Looking at the PATH variable, it can be seen that Anaconda adds its paths to the top of the path list (even though they are already further down the list as inhereited from the system environment variables). So the order of the environment variables matters as the environment will stop at the first instance of whatever command it is looking for. So the fix is to put the Anaconda paths to the top of my environment variables. This means I can run TexStudio from the usual shortcut and the correct paths are used!

Related Question