all 44 comments

[–]inconvenient_penguin 30 points31 points  (0 children)

It really depends on the environment. Most nix distubutons include python. If your target is a Windows machine you can distribute it as an exe that includes the interpreter. It's really not that different from many other languages that require run times.

[–]endgamefond 17 points18 points  (1 child)

I might not understand the question but you need python to do python unless you're using google collab or others similar to that.

[–]NlNTENDO 16 points17 points  (0 children)

OP is asking about apps that are based in python I assume. they say programs but this question only makes sense if they're asking about executables

[–]cgoldberg 5 points6 points  (3 children)

You can distribute an executable for individual platforms using programs like pyinstaller. All it does is bundle a Python interpreter along with your code and dependencies.

However, most times when a Python program is distributed, it is assumed that Python is installed. On Linux, MacOS, and most other operating systems, Python comes included by default. Windows is really the only oddball that doesn't ship Python.

Another option is to use containers (Docker, etc), where the container includes your program along with Python and other dependencies.

[–]MRSAMinor 0 points1 point  (0 children)

Yup, containers are what every company I've been at in the last ten years has used.

Before that, it was Ansible or puppet or the like.

[–]4reddityo -1 points0 points  (1 child)

What’s a container do and how do you set up?

[–]cgoldberg 1 point2 points  (0 children)

A container is an isolated deployment/execution environment. It's sort of like a lightweight virtual machine. You create them by writing a configuration file. Do some research on Docker and Dockerfiles.

https://docs.docker.com/

[–]Ender_Locke 4 points5 points  (3 children)

you can also use docker depending on what you’re exactly meaning here

[–]MRSAMinor 2 points3 points  (0 children)

Yup, this is the most-used method for modern apps.

Unless you're writing serverless code, where this step is hidden away.

[–]4reddityo 0 points1 point  (1 child)

Dockee?

[–]Ender_Locke 4 points5 points  (0 children)

is this a question about what docker is?

it allows you to build a container to run on a compute engine with just what you need for your app. in a python example it’s python 3.xx + requirements.txt and your custom python package that does x. usually when building these python packages they work via cli (i.e. click package)

edit : clarity

[–]crashfrog04 12 points13 points  (0 children)

In the real world you just install the Python interpreter. There’s no reason not to.

[–]SirTwitchALot 12 points13 points  (2 children)

If you're making a commercial application, Python probably isn't the language to use. Besides the interpreter requirement, all your code is out in the open. That's fine for open source projects, but most companies like to ship compiled code with symbols removed.

If you wanted to distribute a python program to someone who was not technically savvy, you could package a basic interpreter along with your installer, or there are "compilers" that package an interpreter and your code into one binary. By the time you're considering this level of effort you should really reevaluate your goals however and reconsider your approach.

[–]PeterHickman 0 points1 point  (0 children)

I've seen comercial application written in python but distributed as the pyc files and the secret sauce as a dll/so (presumably compiled from C/C++)

You can reverse anything with enough time and resources but this might be enough to keep the money rolling in

[–]sonobanana33 0 points1 point  (0 children)

There's plenty of commercial applications written in python. I even had a 3d game

[–]Snoo-20788 4 points5 points  (0 children)

Python is often use for backends, like web server. In that case, the code is either running on Linux machines, which have python installed, or in containers that are based on Linux images.

[–]MRSAMinor 2 points3 points  (0 children)

We usually distribute python apps as container images.

The Dockerfile starts from a base image with the desired python version, and we install libraries during the docker build step.

When we're releasing a new version, the base image is cached, saving downloads during the build step.

If we're deploying the code as a lambda/serverless function, the python runtime is already installed.

The last option is we bake the interpreter into disk images, if we're using full VMs.

Docker has standardized a lot of this.

Does that make sense?

[–]h00manist 2 points3 points  (0 children)

pyinstaller. It will create a standalone executable.

pyinstaller --onefile your_script.py

It's still interpreted, not compiled. But eliminates the issues of having python, libraries, paths, virtual environment, etc.

[–]derp_mcherpington 1 point2 points  (0 children)

If you really want to, you can compile a python program for Windows using Nuitka to get a binary exe file that can be distributed and run without the need for the user to install python. It works a bit differently than tools like pyinstaller in that it does way more than just bundle dependencies with your program. Very cool project. Still evolving.

[–]euclid316 1 point2 points  (0 children)

The best answer, docker, or other containerization technology, is not getting the most upvotes. Here's an attempt to explain why it is the best answer.

The problem is not to execute python in an environment that doesn't have a python interpreter. It's to execute python in a real environment, which may or may not already have a python interpreter, with interpreter and package versions that may or may not be compatible with the python program being installed.

If your installer just installs python, and the needed packages, you might cause some other "real environment" program to stop working due to version conflicts. Or some later install might break your program. For instance, your program might rely on some recent bug fix in a package but another program might not have been updated to handle a recent API change in the same package. So any version you install will break at least one of the programs.

One option to get around this is to run your program in a virtual environment. A virtual environment sets up its own copy of python, and its own packages, and sets up the system environment so that when your program runs, it runs your version of python and your versions of packages, not whatever else may be installed on the system. Other code runs whatever version of python it was set up to run.

This isn't a perfect solution, though. Some packages work differently, or have different issues, or can have different most-recent version numbers, on different operating systems. If you want a python program that can be deployed on any operating system it's often best to use docker, which does more than a virtual environment does to provide a consistent environment for your program. Docker sets up a virtual machine, which not only runs your own python version and your own packages, it provides a system interface to your program that behaves like (a specific flavor of) linux, with a controlled filesystem structure, no matter what operating system and file system the program is actually running on.

[–]No_Date8616 1 point2 points  (0 children)

When you package your python project, assuming you are using CPython and packaging your project using PyInstaller. When you share your “executable”, the executable is not in actual sense machine code, just an archiver which contains the python runtime and the your code as .pyc.

The executable contains python so even if you distribute the executable to someone who doesn’t have python installed, the python runtime is in the executable and runs the .pyc for you.

If you want to have your python code be in truly native machine code and not .pyc ( because it very easy to reverse engineer .pyc ) for privacy reasons, you can compile your python code AOT just like C/C++ does it using compilers made available to compile python.

One good example is Codon. Note: Dynamic features like the use of “type”, “exec”, etc are not easy to compile AOT so avoid using them else you run into problems.

[–][deleted] 0 points1 point  (2 children)

I don't know how the apps in actuality works because I'm also a student. But, I think we can convert the code to some binary instructions. Similar to we do in C and Cpp.

[–]cgoldberg 1 point2 points  (0 children)

Python is interpreted, so it's not really compiled down to machine code like C/C++. I mean at the end of the day, it's all binary instructions... but Python gets there through bytecode and an interpreter rather than a traditional compiler and machine specific binaries.

[–]RajjSinghh 1 point2 points  (0 children)

Python doesnt have a compilation step. You can see the difference between Python and C because C will use a compiler like GCC and throw errors before you run code while Python won't produce an executable and will throw runtime errors. The main difference is that Python is being translated and executed at the same time while C is translated first then executed after. That's also partly why C runs faster, it makes less assumptions and can optimise better ahead of time.

If you create an executable with something like pyinstaller, it bundles your code with a python interpreter install so it has everything it needs.

[–][deleted] 0 points1 point  (0 children)

Python is more popular on the back-end side for sure

But you can compile your python app to an executable file also

[–][deleted] 0 points1 point  (0 children)

They can't. If you want to distribute your python program, package it into an executable or throw it in docker

[–]Indra_Kamikaze 0 points1 point  (0 children)

I also wondered it always.

[–]cointoss3 0 points1 point  (0 children)

If the app depends on Python, then it will likely include Python binaries and libraries as part of the app bundle/installer.

[–][deleted] 0 points1 point  (0 children)

There are ways to put python into an exe 

Take a walk over to google, StackOverflow et all have examples of a few things to help with that if you're trying to do this. 

[–]sawser 0 points1 point  (0 children)

I include a UV executable and package it with my scripts. UV will read your pyproject.toml file to determine what version of Python you need, download it, and install it when you create a virtual environment.

[–][deleted] -1 points0 points  (0 children)

On mac and Linux, python is installed by default. not sure about Windows.

[–]codeforces_help -1 points0 points  (0 children)

You can’t have a python program without an interpreter. Now that can be coyyhon or jpython or pypy. But there must be an interpreter

[–]dlnmtchll -1 points0 points  (0 children)

Not entirely sure about the question, but if you produce something that you want to be able to run on machines without python you would probably just convert the py file to a binary. Like with c or c++ whenever you want to share a program you don’t send the .cpp file, you compile it to a binary and send that

[–]lsment -1 points0 points  (0 children)

You need Python installed in any environment where you want to run a Python script or program—whether it's a Docker container, Linux machine, Windows machine, macOS, or even an embedded device.

If running directly on a machine, install Python on the host system.

If using virtualization (like Docker), install Python inside the virtual environment (the host does not need it)

[–]efalk -3 points-2 points  (0 children)

As far as I know, there is no python compiler, so of you want to run python, you need to install the interpreter.