17

Can I create a Python program, send it to a remote computer, and run it there without that computer having Python installed? I've heard that you cannot, as Python needs to be interpreted. If this is true, then it seems very odd as it would be hard to distribute your program unless everyone decides to install Python.

Also, what about C and C++? Can they be run on a remote computer without having the language installed? (I think you can, as it is a compiled language).

I'm not exactly sure and would like clarification.

EDIT:

I'm getting some mixed answers on this and am not sure where to go. I see that I can include the Python library in the program and I can use py2exe.

However, I'm confused on C and C++. Do I have to include their libraries in the program? Can they only be run on certain machines? Does the compiler allow it to run on all machines?

merlin2011
  • 71,677
  • 44
  • 195
  • 329
Justin
  • 179
  • 1
  • 1
  • 4

7 Answers7

12

Look at py2exe and py2app for Windows and Mac. Macs running OSX and most modern Linuces have Python installed, however.

C/C++ apps are normally compiled to executables which work on one machine/OS architecture (e.g. 32-bit Windows, or 64-bit OSX); such an executable can run on some but not all machines. For example, 64-bit Windows or OSX can run programs built either for the 32-bit or 64-bit flavor of their respective OSes.

Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
  • Okay, so I can use py2exe to distribute Python. What about C and C++. I'm getting mixed answers. How would I go about distributing it amongst any computer? – Justin Feb 04 '11 at 02:56
  • 3
    @Justin: "run on any computer" is not happening. Not all processors use the same instruction set. At most one processor family will be able to run your program without needing special translation runtime support files, and in order to do much of anything useful, you need OS-specific code as well. Running on more than one system will always need a system-specific runtime helper. – Ben Voigt Feb 04 '11 at 05:08
9

python is interpreted, so it won't run without python. However, that doesn't mean that python has to be installed, you can include a copy in your program directory or even bundle your program and the python runtime into a single file.

C and C++ compilers toolchains generate machine code (in most cases, C interpreters do exist, as do C and C++ -> p-code and bytecode compilers). But most C and C++ programs use shared libraries, and will not run unless the shared library is present (again, doesn't have to be installed, can be placed in the program directory). There's also usually a build option (static linking) to include all necessary libraries in the main program file.

But the result is still limited to a particular combination of OS and CPU architecture. Getting a program to run on more than a single platform always requires platform-specific runtime support.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

You can use py2exe for distributing Python programs to Windows.

http://www.py2exe.org/

2

If a you have written a program in any language, and that program is not compiled to machine code, something on the user's computer must convert it to machine code before it can be run.

In the case of JavaScript, that "something" is often a web browser. In the case of Python, that is often a stand-alone interpreter, though it is possible to compile it:

Is it feasible to compile Python to machine code?

However, to be clear: just because your program is not compiled to imachine code does not mean that it will be interpreted. Programs written in C# are usually compiled to MSIL, which is compiled to machine code the first time the program is run. Java programs are also compiled when they are first run.

Community
  • 1
  • 1
ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
2

I will give a practical application of sending code to a remote machine to run. This is typically done in the BOINC project, a community GRID computing initiative which has produced gems such as SETI@Home. The applications typically are compiled C++ versions with multi-platform binaries for x86-linux, AMD64-linux, win32, win64 and Mac OS Universal Binaries (with ppc,x86 and 64-bit). This is a lot of variety for distribution, but a modern make system can easily automate all that (e.g. CMake).

A lot of people prefer the WORA method (write once run anywhere) and stick with VM based language like Java or Python. In this case the boinc projects distribute a version of the VM as well as the code to run on it. Java VM's being encumbered with licensing issues, Python VM is much nicer. Boinc is attempting to embed the Python VM in various BOINC clients to make the distribution of Python based GRID applications easier.

I hope this gives you an idea about application distribution and helps you make an informed decision.

whatnick
  • 5,400
  • 3
  • 19
  • 35
1

There is a py2exe that can produce an executable that will run on another computer without that user installing the normal Python package.

Yes, C and C++ are (at least normally) implemented as compilers that can produce standalone executables.

Edit: In a typical case, a C or C++ implementation will link the functions from the standard library that are used in the program into the executable. This can (and often does) include quite a bit that's not used directly, but still doesn't normally include (anywhere close to) the entire standard library.

In most cases you can also produce an executable that depends on an implementation of the standard library already being present on the target machine in the form of a shared library, DLL, etc. (different OSes use different names). This reduces the size of the executable, but increases the headaches involved in distribution; I use it for code I'm compiling on my own machine, but generally avoid it when/if I'm distributing an executable to anybody else. Given current hard drive prices, the savings in disk space is rarely worth the headache.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • For a weak definition of "standalone" which means "dependent only on a particular OS and set of shared libraries already being present on the computer". And yes this is the *usual* definition of standalone in computer programming, I just wanted to highlight that it's different from the literal English meaning. – Ben Voigt Feb 04 '11 at 15:22
  • @Ben Voigt: If you're going to get pedantic about it, I'd still have to disagree (though the original statement is incorrect as well). The compiler produces only an object file. It's up to the linker to take things from there -- and while most are typically configured to produce output that depends on an OS (etc.) it's also possible (in at least some cases) to produce truly standalone results (i.e., will run as boot code). From his question, he's targeting Windows though... – Jerry Coffin Feb 04 '11 at 15:27
  • Perhaps you would enlighten me as to what in the question says Windows (vs Mac OSX or even iPad)? I agree that it's not likely to be linux, where users have a better concept of OS-specificity. And yes, you have a point with compiler vs toolchain. – Ben Voigt Feb 04 '11 at 15:30
  • @Ben Voigt: Re-reading it, you're right. I was going from what I remembered from reading it last night -- I should know better than to do something like that. – Jerry Coffin Feb 04 '11 at 15:32
0

Look into Pyinstaller for standalone executables with no python integration needed. Well, apart from the crucial libraries so it can run!

It's recently updated, well maintained and even supports cython integration though that can get complex. You can compress the files to be smaller or if you have multiple executables, you can link them to one file to reduce size.

You can also of course create a single executable with python installed. Don't use anaconda though (use default python 3.6) to ensure your program is very small in size.

Hope this helps.

Tetora
  • 433
  • 8
  • 25