Just to do a bit of advertisement for my robust-externalize library, here is a solution. Note that this library is not just made for python, so it might be less specialized than above libraries, but it will be certainly significantly more flexible as it can:
- cache any sort of content, including images (python generated, online…),
- can deal with any language,
- can be used with or without shell-escape for the first compilation (no need to compile twice with shell-escape), no shell-escape needed for later compilations (cached result)
- provide functions to compile in parallel, clean the cache,
- can also get variables from latex via
set placeholder eval={__foo__}{\foo}.
- print the code next to the result
- automatically run some code (python imports…) if some string are found…
- compilation can be externalized to some servers (say you need GPU access, special libraries…)
- it works on overleaf, arxiv…
If you don't have a recent enough CTAN (needs version v2.7 for the indentation not to cause any issue), just copy the .sty file from https://github.com/leo-colisson/robust-externalize/ in your project.
Version 1: just compute some variables in python and reuse them later
It is simpler to compile with pdflatex --shell-escape document.tex, but not mandatory, see doc.
\documentclass{article}
\usepackage{robust-externalize}
\robExtConfigure{
enable fallback to manual mode, % avoid error if shell escape is forgotten/not used, print instead the command in the pdf
}
\begin{document}
\begin{CacheMeCode}{python, do not include pdf}
import math
myvalue = math.sqrt(37)
write_to_out(r"\gdef\myvalue{" + str(myvalue) + r"}")
\end{CacheMeCode}
The value we computed via the above python code is \myvalue.
\end{document}

Version 2: make this into a tiny macro
This library does not provide directly a single macro to do the above in one line (maybe I should provide this option at some points), but it is fairly easy to make one, and you can customize it at will, for instance to wrap your code with some functions automatically, wrap the output with some tikz figures etc:
\documentclass{article}
\usepackage{robust-externalize}
\robExtConfigure{
enable fallback to manual mode, % avoid error if shell escape is forgotten/not used, print instead the command in the pdf
new preset={my python exec}{
python,
custom include command={\mytmpvalue},
add import={import math},
set placeholder={ROBEXT_MAIN_CONTENT}{write_to_out(r"\gdef\mytmpvalue{" + str(ROBEXT_MAIN_CONTENT_ORIG)+ r"}")}
},
}
\begin{document}
You can compute simple values like \cacheMe[my python exec]{math.sqrt(42)}.
\end{document}

Version 3: include both the code and the result
We provide directly a preset for this (NB: make sure to use v2.8 or above as 2.7 introduces a bug where an empty line is added in front of the code):
\documentclass{article}
\usepackage{robust-externalize}
\usepackage{pythonhighlight}
\usepackage[most]{tcolorbox}
\robExtConfigure{
enable fallback to manual mode, % avoid error if shell escape is forgotten/not used, print instead the command in the pdf
}
\begin{document}
\begin{CacheMeCode}{do not remove leading spaces, python print code and result, set title={The for loop}}
for name in ["Alice", "Bob"]:
print(f"Hello {name}")
\end{CacheMeCode}
\end{document}

Version 4: matplotlib images
For a basic pdf-based rendering, you can use the following code, for a more advanced integration with LaTeX with a pgf backend, see my answer here https://tex.stackexchange.com/a/710138/116348
\documentclass{article}
\usepackage{robust-externalize}
\usepackage{pythonhighlight}
\usepackage[most]{tcolorbox}
\robExtConfigure{
enable fallback to manual mode, % avoid error if shell escape is forgotten/not used, print instead the command in the pdf
}
\begin{document}
\begin{figure}[ht]
\centering
\begin{CacheMeCode}{python, add to includegraphics options={width=\linewidth}}
import matplotlib.pyplot as plt
import matplotlib
year = [2014, 2015, 2016, 2017, 2018, 2019]
tutorial_count = [39, 117, 111, 110, 67, 29]
plt.plot(year, tutorial_count, color="#6c3376", linewidth=2)
plt.title("Simple plot")
plt.xlabel('Year')
plt.ylabel('Number of futurestud.io Tutorials')
print(get_filename_from_extension(".pgf"))
plt.savefig("ROBEXT_OUTPUT_PDF")
\end{CacheMeCode}
\caption{Test}%
\end{figure}
\end{document}

\tdegfin your main document (outside\begin{python}...\end{python}) and it should print the equivalent of\tdegcin Fahrenheit? As in-3.889? By the way, your Fahrenheit calculation is incorrect:tdegf = (tdegc - 32.0)*(5/9). – Werner Oct 30 '11 at 15:51