I come to you this time not with a bit of code but with a large question:

Is there a simple way to run some python code inside my document ?

I came across some options :

  • Sage : Quite a heavy installation... and in the end couldn't make it work.
  • An outdated file known as : python.sty, which seems to have been partially erased from the internet (many pages about it simply give a 404 error). I saw it here :https://texample.net/weblog/2008/oct/24/embedding-python-latex/ and for sure it looked promissing. It's supposed to write the code you put inside a python environnement into a file and run it, sounds too good to be true... And if it did work at some point (even in online compiling it seems), it doesn't now. I found a version of the file on an old github directory, it runs partially, but gives some compilation errors like "Missing number, treated as zero."... Some discussion suggested that it was redundant with LuaTex, I looked that up and can't really see that. It was known here too : Latex-Python combination not running on a mac
  • Right now I am having a look at Pythontex

So, any suggestion ? The best would be something I could use on Overleaf (I am allowed to dream right ?)

Why ? Because I just ended a project I wanted to do, in which my compilation does some calculations. I have met LaTeX weaknesses and limitations, and things that seem that they would be the easiest in python are really not in LaTeX.

Thank you for reading.

  • 2
    pythontex is probably the way to go. But do you need the code to execute inside TeX? I usually can get by with saving the output of my script somewhere and then compiling with TeX as a two step process.
    – Teepeemm
    May 24, 2021 at 19:24
  • 1
    I usually set up a Makefile that would execute any Python script I need the results of if necessary.
    – Skillmon
    May 24, 2021 at 19:32
  • 2
    Get a free Cocalc account. This gives you Sage without having Sage on your computer. You just need the internet connection. Sage gives you Python plus a computer algebra system which means you won't have to "reinvent the wheel": lots of mathematics is built in.
    – DJP
    May 24, 2021 at 21:26

1 Answer 1


You could use pythontex or just call python by hand, here is python running on overleaf calculating 1+2=3 (there are simpler ways of adding)

enter image description here

The tex code (which needs --shell-escape but that is automatic at overleaf) is



\input{|python -c 'print(1+2)'}


This also works with multi-line python scripts:

enter image description here


\input{|python mypy.py}


with mypy.py being

print("\\section{The First}\n")

print("Some colours:\n\n")


for c in clr:
    print("\n\\textcolor{" + c + "}{" + c +"}\n")

Example running on Overleaf (although I don't promise to keep that running forever, so here's a screenshot as well)


enter image description here

  • 1
    @LMT-PhD you could write a python script using filecontents or \write then specify the filename rather than an inline expression to python. May 25, 2021 at 12:42
  • 1
    Would you have an example so I could close that question ? I found one but can't make it compile
    – LMT-PhD
    May 25, 2021 at 15:33
  • 1
    @BillN you need the * form or you will get TeX comments in your python I extended the example a bit at overleaf.com/read/gpfndyfcrhhr Jun 22, 2021 at 14:43
  • 1
    Hi @DavidCarlisle. Can you highlight the part "The tex code (which needs --shell-escape but that is automatic at overleaf) is". As it is required if you're running latex locally. Thanks for the answer. you saved my day. Jul 21, 2023 at 13:53
  • 2
    @AsifMehmood done Jul 21, 2023 at 14:01

Not the answer you're looking for? Browse other questions tagged .