python程序可以在没有python的电脑上运行吗?C/C++ 呢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4894048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a python program be run on a computer without Python? What about C/C++?
提问by Justin
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.
我可以创建一个 Python 程序,将它发送到远程计算机,然后在没有安装 Python 的计算机的情况下在那里运行它吗?我听说你不能,因为 Python 需要被解释。如果这是真的,那么这似乎很奇怪,因为除非每个人都决定安装 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).
另外,C 和 C++ 怎么样?它们可以在没有安装语言的情况下在远程计算机上运行吗?(我认为你可以,因为它是一种编译语言)。
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.
我在这方面得到了一些混合的答案,不知道该去哪里。我看到我可以在程序中包含 Python 库,并且可以使用 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?
但是,我对 C 和 C++ 感到困惑。我是否必须在程序中包含他们的库?它们只能在某些机器上运行吗?编译器是否允许它在所有机器上运行?
回答by Pedro Matiello
You can use py2exe for distributing Python programs to Windows.
您可以使用 py2exe 将 Python 程序分发到 Windows。
回答by Russell Borogove
Look at py2exe and py2app for Windows and Mac. Macs running OSX and most modern Linuces have Python installed, however.
查看适用于 Windows 和 Mac 的 py2exe 和 py2app。然而,运行 OSX 的 Mac 和大多数现代 Linuces 都安装了 Python。
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.
C/C++ 应用程序通常被编译为可在一台机器/操作系统架构(例如 32 位 Windows 或 64 位 OSX)上运行的可执行文件;这样的可执行文件可以在某些但不是所有机器上运行。例如,64 位 Windows 或 OSX 可以运行为其各自操作系统的 32 位或 64 位风格构建的程序。
回答by Jerry Coffin
There is a py2exethat can produce an executable that will run on another computer without that user installing the normal Python package.
有一个py2exe可以生成可以在另一台计算机上运行的可执行文件,而该用户无需安装普通的 Python 包。
Yes, C and C++ are (at least normally) implemented as compilers that can produce standalone executables.
是的,C 和 C++(至少在正常情况下)被实现为可以生成独立可执行文件的编译器。
Edit: In a typical case, a C or C++ implementation will link the functions from the standard library that are used in the programinto 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.
编辑:在典型情况下,C 或 C++ 实现会将程序中使用的标准库中的函数链接到可执行文件中。这可以(并且经常)包括很多未直接使用的内容,但通常仍然不包括(任何接近)整个标准库。
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.
在大多数情况下,您还可以生成一个可执行文件,该文件依赖于以共享库、DLL 等形式存在于目标机器上的标准库的实现(不同的操作系统使用不同的名称)。这减少了可执行文件的大小,但增加了分发中的麻烦;我将它用于我在自己的机器上编译的代码,但通常在/如果我将可执行文件分发给其他人时避免使用它。鉴于目前的硬盘价格,磁盘空间的节省很少值得头疼。
回答by ClosureCowboy
If a you have written a program in any language, and that program is not compiled to machine code, somethingon the user's computer mustconvert 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:
在 JavaScript 的情况下,那个“东西”通常是一个网络浏览器。在 Python 的情况下,这通常是一个独立的解释器,尽管可以编译它:
Is it feasible to compile Python to machine code?
However, to be clear: just because your program is not compiled to imachine code does notmean 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.
然而,要明确一点:仅仅因为你的程序没有编译imachine代码并不会意味着它会被解释。用 C# 编写的程序通常被编译成MSIL,它在程序第一次运行时被编译成机器码。Java 程序在第一次运行时也会被编译。
回答by Ben Voigt
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.
python 被解释,所以它不会在没有 python 的情况下运行。但是,这并不意味着必须安装 python,您可以在程序目录中包含一个副本,甚至将您的程序和 python 运行时捆绑到一个文件中。
C and C++ compilerstoolchains 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.
C 和 C++编译器工具链生成机器代码(在大多数情况下,C 解释器确实存在,C 和 C++ -> p 代码和字节码编译器也是如此)。但是大多数 C 和 C++ 程序使用共享库,除非共享库存在,否则不会运行(再次,不必安装,可以放在程序目录中)。通常还有一个构建选项(静态链接)来在主程序文件中包含所有必要的库。
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.
但结果仍然仅限于操作系统和 CPU 架构的特定组合。让程序在多个平台上运行总是需要特定于平台的运行时支持。
回答by whatnick
I will give a practical application of sending code to a remote machine to run. This is typically done in the BOINCproject, 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).
我将给出一个将代码发送到远程机器运行的实际应用。这通常在BOINC项目中完成,这是一个社区 GRID 计算计划,它产生了诸如 SETI@Home 之类的宝石。应用程序通常是编译的 C++ 版本,带有用于 x86-linux、AMD64-linux、win32、win64 和 Mac OS 通用二进制文件(带有 ppc、x86 和 64 位)的多平台二进制文件。分发的种类很多,但是现代的 make 系统可以轻松地自动化所有这些(例如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 embedthe Python VM in various BOINC clients to make the distribution of Python based GRID applications easier.
许多人更喜欢 WORA 方法(一次编写,随处运行)并坚持使用基于 VM 的语言,如 Java 或 Python。在这种情况下,boinc 项目分发 VM 的一个版本以及在其上运行的代码。Java VM 受到许可问题的困扰,Python VM 要好得多。Boinc 正在尝试将Python VM嵌入到各种 BOINC 客户端中,以简化基于 Python 的 GRID 应用程序的分发。
I hope this gives you an idea about application distribution and helps you make an informed decision.
我希望这能让您了解应用程序分发并帮助您做出明智的决定。
回答by Tetora
Look into Pyinstaller for standalone executables with no python integration needed. Well, apart from the crucial libraries so it can run!
查看 Pyinstaller 以获取不需要 python 集成的独立可执行文件。好吧,除了关键的库之外,它可以运行!
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.
它最近更新,维护良好,甚至支持 cython 集成,尽管这可能会变得复杂。您可以将文件压缩得更小,或者如果您有多个可执行文件,您可以将它们链接到一个文件以减小大小。
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.
您当然也可以创建一个安装了 python 的可执行文件。不要使用 anaconda(使用默认的 python 3.6)来确保你的程序非常小。
Hope this helps.
希望这可以帮助。

