I have the following MWE :
\documentclass{article}
\usepackage{pythontex}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\linewidth}{X}
\pygment{c}{#include <stdio.h>} \\
\end{tabularx}
\end{document}
which give me
Illegal parameter number in definition of \pytx@argdetok.
Illegal parameter number in definition of \pytx@arg.
Illegal parameter number in definition of \pytx@argdetok.
Illegal parameter number in definition of \pytx@arg.
Illegal parameter number in definition of \pytx@argdetok.
Illegal parameter number in definition of \pytx@arg.
I studied a bit the behavior of \pygment{c}{#include <stdio.h>} :
- This line works outside
tabularxenvironment - If I remove
#in#include, the error goes away.
Side notes :
- Compilation :
pdflatex main.tex && pythontex main.tex && pdflatex main.tex - It seems to be a known issue, see this and this answer of G. Poore.

#safe up so it is not in the argument of any command (normally environments are Ok but tabularx is "special") but not so early that it comes before and definitions like\newcomand\foo[1]{zzz #1}which need the special#behaviour. so in your example, it can not be done when pythontex is loaded (as that would break\usepackage{tabularx}) and it can not be done by the\pygmentcommand as that is already insidetabularxso it is too late to change#– David Carlisle Aug 11 '18 at 21:01