python 在单独的文件夹中编译python文件的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/471928/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:11:26  来源:igfitidea点击:

Way to have compiled python files in a separate folder?

pythonfilecompiled

提问by Evan Fosmark

Is it possible to have Python save the .pycfiles to a separate folder location that is in sys.path?

是否可以让 Python 将.pyc文件保存到单独的文件夹位置sys.path

/code
    foo.py
    foo.pyc
    bar.py
    bar.pyc

To:

到:

/code
   foo.py
   bar.py
/code_compiled
   foo.pyc
   bar.pyc

I would like this because I feel it'd be more organized. Thanks for any help you can give me.

我喜欢这个,因为我觉得它会更有条理。感谢你给与我的帮助。

采纳答案by jfs

Update:

更新:

In Python 3.8 -X pycache_prefix=PATHcommand-line option enables writing .pycfiles to a parallel tree rooted at the given directory instead of to the code tree. See $PYTHONPYCACHEPREFIXenvvarcredits: @RobertT' answer

在 Python 3.8-X pycache_prefix=PATH命令行选项中,可以将.pyc文件写入以给定目录为根的并行树,而不是代码树。请参阅$PYTHONPYCACHEPREFIXenvvar积分:@RobertT' 回答

The location of the cache is reported in sys.pycache_prefix(Noneindicates the default location in __pycache__[since Python 3.2] subdirectories).

缓存的位置报告在sys.pycache_prefix(None表示__pycache__[自 Python 3.2] 子目录中的默认位置)。

To turn off caching the compiled Python bytecode, -Bmay be set, then Python won't try to write .pycfiles on the import of source modules. See $PYTHONDONTWRITEBYTECODEenvvarcredits: @Maleev's answer

要关闭缓存编译后的 Python 字节码,-B可以设置,然后 Python 不会尝试.pyc在源模块的导入上写入文件。请参阅$PYTHONDONTWRITEBYTECODEenvvar积分:@Maleev 的回答



Old [Python 2] answer:

旧的 [Python 2] 答案:

There is PEP 304: Controlling Generation of Bytecode Files. Its status is Withdrawnand corresponding patchrejected. Therefore there might be no direct way to do it.

PEP 304:控制字节码文件的生成。它的状态是Withdrawn和相应的补丁被拒绝。因此,可能没有直接的方法来做到这一点。

If you don't need source code then you may just delete *.pyfiles. *.pycfiles can be used as is or packed in an egg.

如果您不需要源代码,那么您可以删除*.py文件。*.pyc文件可以按原样使用,也可以装在鸡蛋中。

回答by Charles Merriam

In the dark and ancient days of 2003, PEP 304 came forth to challenge this problem. Its patch was found wanting. Environment variable platform dependencies and version skews ripped it to shreds and left its bits scattered across the wastelands.

在 2003 年黑暗而古老的日子里,PEP 304 的出现挑战了这个问题。它的补丁被发现缺乏。环境变量平台依赖性和版本偏差将其撕成碎片,并将其碎片散落在荒地中。

After years of suffering, a new challenger rose in the last days of 2009. Barry Warsaw summoned PEP 3147 and sent it to do battle, wielding a simple weapon with skill. The PEP crushed the cluttering PYC files, silenced the waring Unladen Swallow and CPython interpreter each trying to argue its PYC file should be triumphant, and allowed Python to rest easy with its dead ghosts occasionally running in the dead of night. PEP 3147 was found worthy by the dictator and was knighted into the official roles in the days of 3.2.

经过多年的磨难,2009 年的最后几天,新的挑战者崛起了。巴里华沙召唤了 PEP 3147 并派它去战斗,挥舞着简单而熟练的武器。PEP 粉碎了杂乱无章的 PYC 文件,使警告的 Unladen Swallow 和 CPython 解释器保持沉默,每个解释器都试图争论其 PYC 文件应该是胜利的,并允许 Python 安息,它的死鬼偶尔会在深夜运行。PEP 3147 被独裁者认为值得,并在 3.2 时代被封为官方角色。

As of 3.2, Python stores a module's PYC files in __pycache__under the module's directory. Each PYC file contains the name and version of the interpreter, e.g., __pycache__/foo.cpython-33.pyc. You might also have a __pycache__/foo.cpython-32.pyccompiled by an earlier version of Python. The right magic happens: the correct one is used and recompiled if out of sync with the source code. At runtime, look at the module's mymodule.__cached__for the pyc filename and parse it with imp.get_tag(). See the What's New sectionfor more information.

从 3.2 开始,Python 将模块的 PYC 文件存储在__pycache__模块目录下。每个 PYC 文件都包含解释器的名称和版本,例如__pycache__/foo.cpython-33.pyc. 您可能还有一个__pycache__/foo.cpython-32.pyc由早期版本的 Python 编译的。正确的魔法发生了:如果与源代码不同步,则使用正确的并重新编译。在运行时,查看模块的mymodule.__cached__pyc 文件名并使用imp.get_tag(). 有关更多信息,请参阅新增功能部分

TL;DR - Just works in Python 3.2 and above. Poor hacks substitute for versions before that.

TL;DR - 仅适用于 Python 3.2 及更高版本。糟糕的黑客替代了之前的版本。

回答by RobertT

And only almost ten years later, Python 3.8 finally provides support for keeping bytecode in separate parallel filesystem tree by setting environment variable PYTHONPYCACHEPREFIXor using -X pycache_prefix=PATHargument (official doc here).

而仅仅在将近十年之后,Python 3.8 终于通过设置环境变量PYTHONPYCACHEPREFIX或使用-X pycache_prefix=PATH参数(官方文档在这里)提供支持将字节码保存在单独的并行文件系统树中。

回答by Maleev

If you're willing to sacrifice bytecode generation altogether for it, there's a command line flag:

如果您愿意为此完全牺牲字节码生成,则有一个命令行标志:

python -B file_that_imports_others.py

Can be put into IDE's build/run preferences

可以放入 IDE 的构建/运行首选项

回答by Salim Fadhley

I agree, distributing your code as an egg is a great way to keep it organized. What could be more organized than a single-file containing all of the code and meta-data you would ever need. Changing the way the bytecode compiler works is only going to cause confusion.

我同意,将您的代码作为鸡蛋分发是保持其井井有条的好方法。有什么比包含您需要的所有代码和元数据的单个文件更有条理的了。改变字节码编译器的工作方式只会引起混乱。

If you really do not like the location of those pyc files, an alternative is to run from a read-only folder. Since python will not be able to write, no pyc files ever get made. The hit you take is that every python file will have to be re-compiled as soon as it is loaded, regardless of whether you have changed it or not. That means your start-up time will be a lot worse.

如果您真的不喜欢那些 pyc 文件的位置,另一种方法是从只读文件夹运行。由于 python 将无法写入,因此不会生成 pyc 文件。您遇到的问题是每个python文件在加载后都必须重新编译,无论您是否更改过它。这意味着您的启动时间会更糟。

回答by dietbuddha

I disagree. The reasons are wrong or at least not well formulated; but the direction is valid. There are good reasons for being able to segregate source code from compiled objects. Here are a few of them (all of them I have run into at one point or another):

我不同意。原因是错误的,或者至少没有很好地表述;但方向是有效的。能够将源代码与编译对象分离是有充分理由的。以下是其中一些(所有这些我都曾遇到过):

  • embedded device reading off a ROM, but able to use an in memory filesystem on RAM.
  • multi-os dev environment means sharing (with samba/nfs/whatever) my working directory and building on multiple platforms.
  • commercial company wishes to only distribute pyc to protect the IP
  • easily run test suite for multiple versions of python using the same working directory
  • more easily clean up transitional files (rm -rf $OBJECT_DIR as opposed to find . -name '*.pyc' -exec rm -f {} \;)
  • 嵌入式设备读取 ROM,但能够使用 RAM 上的内存文件系统。
  • 多操作系统开发环境意味着共享(使用 samba/nfs/whatever)我的工作目录并在多个平台上构建。
  • 商业公司希望只分发 pyc 来保护 IP
  • 使用相同的工作目录轻松地为多个版本的 python 运行测试套件
  • 更容易清理过渡文件(rm -rf $OBJECT_DIR 而不是 find .-name '*.pyc' -exec rm -f {} \;)

There are workarounds for all these problems, BUT they are mostly workarounds NOT solutions. The proper solution in most of these cases would be for the software to accept an alternative location for storing and lookup of these transitional files.

所有这些问题都有解决方法,但它们大多是解决方法而不是解决方案。在大多数这些情况下,正确的解决方案是让软件接受用于存储和查找这些过渡文件的替代位置。

回答by Riccardo Galli

Since Python 3.2 has been implemented PEP 3147: this means that all .pyc files are generated inside a __pycache__directory (there will be a __pycache__directory for each directory where you have Python files, and it will hold .pyc files for each version of Python used on the sources)

由于 Python 3.2 已经实现了PEP 3147:这意味着所有 .pyc 文件都在__ pycache__目录中生成(每个包含 Python 文件的目录都有一个__ pycache__目录,它将保存 .pyc 文件用于源代码中使用的每个 Python 版本)

回答by jb.

There is ongoing pep that will enable building bytecode to magic directory.

正在进行的 pep 将能够字节码构建到魔术目录

Basically all python files will be compiled to directory __pythoncache__.

基本上所有的python文件都会被编译到目录中__pythoncache__

回答by Cesar Canassa

For Python 3.8 or higher:

对于 Python 3.8 或更高版本:

The PYTHONPYCACHEPREFIXsetting (also available as -Xpycache_prefix) configures the implicit bytecode cache to use a separate filesystem tree, rather than the default __pycache__subdirectories within each source directory.

PYTHONPYCACHEPREFIX设置(也可用作-Xpycache_prefix)将隐式字节码缓存配置为使用单独的文件系统树,而不是__pycache__每个源目录中的默认子目录。

The location of the cache is reported in sys.pycache_prefix(Noneindicates the default location in __pycache__subdirectories).

缓存的位置报告在sys.pycache_prefix(None表示__pycache__子目录中的默认位置)。

回答by S.Lott

"I feel it'd be more organized" Why? How? What are you trying to accomplish?

“我觉得它会更有条理” 为什么?如何?你想达到什么目的?

The point of saving the compiler output is to save a tiny bit of load time when the module gets imported. Why make this more complex? If you don't like the .pyc's, then run a "delete all the .pyc's" script periodically.

保存编译器输出的目的是在导入模块时节省一点加载时间。为什么要让这更复杂?如果您不喜欢 .pyc,请定期运行“删除所有 .pyc”脚本。

They aren't essential; they're helpful. Why turn off that help?

它们不是必需的;他们很有帮助。为什么要关闭该帮助?

This isn't C, C++ or Java where the resulting objects are essential. This is just a cache that Python happens to use. We mark them as "ignored" in Subversion so they don't accidentally wind up getting checked in.

这不是结果对象必不可少的 C、C++ 或 Java。这只是 Python 碰巧使用的缓存。我们在 Subversion 中将它们标记为“已忽略”,这样它们就不会意外地被签入。