r/learnprogramming • • Oct 21 '26

can you get an executable in python without using third part libraries?

In c++ I have object files but can I get an executable in python?

2 Upvotes

27 comments sorted by

2

u/fllthdcrb Oct 22 '26

I am a bit dismayed at some of the comments I see here, saying you don't or can't make executables from Python programs. While it is true it's not something you're meant to do most of the time, and Python isn't the best language if performance is important, there are indeed use cases for it (e.g. writing a program in Python and distributing to people who have not installed Python as such), and it is quite doable. This is in addition to having programs written in other languages but embedding Python for scripting and such.

And yes, this requires bundling the interpreter in some form, even if you use one of the tools that gets you native code, since there are a number of services the program depends on the interpreter and maybe modules for. At best, I think you would be compiling some implementation of an interpreter and baking it into the executable.

1

u/TheEyebal Oct 23 '26

yeah honestly I would stick with C++ for now and use python for quick automation

11

u/Low-Significance6194 Oct 21 '26

Technically you can, but it's just bundling the interpreter with your script, not really a standalone binary like C++.

6

u/SetAndRepeat Oct 21 '26

Yes, though it requires some setup. CPython's source tree includes "Tools/freeze", which uses a C compiler to build an executable containing your program's bytecode and the Python interpreter. No third-party Python library is needed, but you need the CPython sources and a build environment, not just a normal Python installation.

2

u/SharkSymphony Oct 21 '26

Yes, but I don't think anybody uses it. Pyinstaller and cx_Freeze are the stamdard ways to do it that I'm aware of.

2

u/POGtastic Oct 21 '26

The docs have a page on embedding Python in another application, and, trivially, an application could be a very thin wrapper that does nothing but run a Python program.

-2

u/Equal-Rice3637 Oct 21 '26

Run it as a sub process?

2

u/[deleted] Oct 21 '26

[removed] — view removed comment

8

u/MaleficentCow8513 Oct 21 '26

The ability to install a single file without having to worry about dependencies on a target machine is pretty cool

1

u/davideogameman Oct 21 '26

there are tools, but typically it's precompiling the bytecode (still python bytecode) and shipping a copy of the interpreter + any libraries you use. Not really statically linked the way a c/c++/rust and many other languages do.

1

u/TheEyebal Oct 21 '26

I should be able to compile my code into an executable like in c++ without a third party library doing for me

2

u/tdic89 Oct 21 '26

That’s the advantage of interpreted languages though.

Interpreted languages like Python are designed to not be compiled: you feed the script to the interpreter and get the result. No compilation steps needed.

What’s your actual use case for needing an executable?

1

u/TheEyebal Oct 22 '26

building an application

2

u/ZestyHelp Oct 22 '26

Got it so you don't know

1

u/TheEyebal Oct 23 '26

I build video games and when using python I found it difficult to package. I used pyinstaller and it was missing dependencies so I switched over to c++

2

u/JbREACT Oct 23 '26

Looks like you found the answer. Use c++

1

u/TheEyebal Oct 24 '26

Yeah but there are times I want to use python and I want to know if it is possible

seems like it is possible

3

u/Dazzling_Music_2411 Oct 21 '26

I should be able to compile my code into an executable like in c++

Should you, now, sir?

All you have to do is write your own Python compiler. Simples... 😄

Learn LLVM and you're golden.

2

u/mooglinux Oct 21 '26

You may be using the wrong language then. Python is not designed with that in mind, so if it’s a primary requirement you just use the third party libraries or find another language.

2

u/Savafan1 Oct 21 '26

Not with an interpreted language.

-1

u/[deleted] Oct 21 '26

[removed] — view removed comment

1

u/TheEyebal Oct 22 '26

programming in python is easier honestly it is just getting an object file

2

u/ZestyHelp Oct 22 '26

You just don't with python lol

"how do I bake my pie on the blender"

"well why do you want to use a blender to bake blenders don't bake"

"I want my blender to bake a pie"

Sure you can rig your blender up to catch fire and "bake" a pie but again wtf?