r/learnpython • • 20h ago

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?

0 Upvotes

24 comments sorted by

7

u/jameyiguess 20h ago

No. You can do some hacky stuff with bundling the cpython runtime with your files, but it's not machine code. 

5

u/ResidentPomegranate6 20h ago

python never makes a real exe, the interpreter is the executable. stdlib zipapp bundles a .pyz, but still needs python installed.

6

u/DTux5249 19h ago

No. Python's an interpreted language.

Unless you get an external library/tool, you don't compile Python.

4

u/DanceHackRock 18h ago

That's not what python is for.

2

u/PhilNEvo 16h ago

Out of curiosity, why?

If you just want a quick icon to doubleclick, just make a script with batch or shell or something, that runs your python code.

•

u/TheEyebal 5m ago

If I make a game and want to put it on itch.io with different OS versions would that be possible with your recommended method

1

u/stickycontentss 10h ago

Without third-party libraries, your realistic stdlib option is zipapp:

```python

myapp/main.py -> the entry point

import myapp myapp.main() ```

bash python -m zipapp myapp -m "myapp:main" -o myapp.pyz

That gives you myapp.pyz, runnable as python myapp.pyz (and executable directly on Unix with a shebang). The catch: the target machine still needs Python installed - zipapp packages your code, not the interpreter.

There's also the plain compileall route (python -m compileall . -> .pyc files), but that's bytecode-only distribution, not an executable.

For a real double-click .exe where Python isn't installed, something has to embed the interpreter, and that something is always third-party:

  • PyInstaller - most common, pyinstaller --onefile myapp.py
  • Nuitka - compiles to C, better start-up and harder-to-read output
  • cx_Freeze / py2exe - older but maintained

There's no stdlib equivalent because bundling an interpreter + platform libs is genuinely a big job.

Practical answer: ship zipapp + a two-line run.bat (python myapp.pyz %*) if you control the machine; PyInstaller if you don't.

1

u/TheRNGuy 13h ago

Can, do not recommend. 

1

u/TheRNGuy 13h ago

Can, do not recommend. Use existing frameworks. 

0

u/acw1668 20h ago edited 19h ago

In Linux:

  • add a valid shebang at the first line of the script
  • set the executable attribute of the script using chmod a+x ...

Then you can get an executable.

-2

u/TheEyebal 20h ago

I can try this?

7

u/GentleWhiteGiant 19h ago

I think you are talking about a binary executable of your code. No, that is not what happens here. The resulting script can be executed on the command line, but it is still your original script. .

-2

u/TheEyebal 19h ago

Oh so it does return an object file

3

u/acw1668 19h ago

No, it does not change the script file to object file, just make the script file executable. Without 3rd-party tool, you cannot convert a Python script to binary executable.

2

u/Javanaut018 19h ago edited 19h ago

The shebang tells the system which interpreter to use. So the file can be run as is.

For a real amd64 executable try codon ur nuitka.

1

u/AmanBabuHemant 20h ago

Pyinstaller and Nuitka are two of populer compiler, you can use them to compile and get different kind of executables.

-3

u/TheEyebal 20h ago

Yeah but is there a way to to compile without pyinstaller or Nuitka?

4

u/AmanBabuHemant 16h ago

Python is an interpreted programming language, that's it, and these tools allow programmers to distribute their software by compiling in a single executable so they can distribute it to either users/client for varies reasons.

1

u/Javanaut018 19h ago

Native executables are compiled. It's the way they are made....

You can convert them to machine code by hand, tho ...

0

u/Confident_Hyena2506 18h ago

Yes - use pyinstaller. This will just bundle python and all the stuff into your exe on windows. You can do similarly on linux but people would look at you funny if called it an exe.

0

u/oclafloptson 14h ago

If a 3rd party library exists then chances are that whatever that library does can also be done without it. The core library doesn't ship with this as a stock feature, though. You'll have to either build it or use a 3rd party system. Given that you have to ask... No one here is going to have great faith in your ability to do the former.

I don't believe that it's right to answer no to this nor to expect others to hand hold you through something of this level of difficulty. This is one of those topics that you need to put down the python-noob sub and start reading actual academic texts. You've got ground to cover before you get to here

-3

u/AromaticCredit1533 20h ago

You can install the Shed Skin compiler for Python (https://pypi.org/project/shed-skin/), it can compile your Python codebase into an exe

1

u/meta-ape 19h ago

Shed Skin supports only a subset of Python, tho. Devs say that it’s practical for some 100 lines of code. I imagine that would mean optimizing parts of your code and using them in your main.

-1

u/AromaticCredit1533 19h ago

Ye, unfortunately it's gonna be difficult. Most Python compilers will be faced with some restrictions. After all, some core features of Python (inspect, ast, disasm, ..) rely on the dynamism of the run-time virtual machine.

Edit: I'm not saying that you can't compile Python. Even if it's mainly an interpreted language, I think it's nice that some people are curious about the possibility of compiling your code to squeeze extra performance. But since so much of the design of Python is centered around the assumption of a runtime, it's not gonna be easy.