我应该将Python的pyc文件添加到.dockerignore吗? [英] Should I add Python's pyc files to .dockerignore?

查看:159
本文介绍了我应该将Python的pyc文件添加到.dockerignore吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了Python项目的 .dockerignore 文件的几个示例,其中 *。pyc 文件和/或 __ pycache __ 文件夹将被忽略:

I've seen several examples of .dockerignore files for Python projects where *.pyc files and/or __pycache__ folders are ignored:

**/__pycache__
*.pyc

由于这些文件/文件夹无论如何都将在容器中重新创建,我想知道是否

Since these files/folders are going to be recreated in the container anyway, I wonder if it's a good practice to do so.

推荐答案

是的,这是推荐的做法。原因有几个:

Yes, it's a recommended practice. There are several reasons:

中。 dockerignore 您指定的文件不会转到生成的图像,这在构建最小图像时可能至关重要。大致来说,字节码文件的大小等于实际文件的大小。字节码文件不是要分发的,这就是为什么我们通常也将它们放入

In .dockerignore you specify files that won't go to the resulting image, it may be crucial when you're building the smallest image. Roughly speaking the size of bytecode files is equal to the size of actual files. Bytecode files aren't intended for distribution, that's why we usually put them into .gitignore as well.

在早期版本的Python 3.x中,有多个缓存的相关的问题

In earlier versions of Python 3.x there were several cached related issues:


Python在.pyc文件中缓存字节码的方案在具有多个Python解释器的环境中不能很好地工作。如果一个解释器
遇到另一个解释器创建的缓存文件,它将
重新编译源文件并覆盖该缓存文件,从而失去
缓存的好处。

Python’s scheme for caching bytecode in .pyc files did not work well in environments with multiple Python interpreters. If one interpreter encountered a cached file created by another interpreter, it would recompile the source and overwrite the cached file, thus losing the benefits of caching.

自Python 3.2开始,所有缓存的文件的解释器版本均以 mymodule.cpython-32.pyc 开头,并在 __ pychache __ 目录。顺便说一下,从Python 3.8开始,您甚至可以控件将存储缓存的目录。当您限制对目录的写访问但仍然希望获得缓存使用的好处时,此方法可能很有用。

Since Python 3.2 all the cached files prefixed with interpreter version as mymodule.cpython-32.pyc and presented under __pychache__ directory. By the way, starting from Python 3.8 you can even control a directory where the cache will be stored. It may be useful when you're restricting write access to the directory but still want to get benefits of cache usage.

通常,缓存系统运行良好,但是总有一天可能会出问题。值得注意的是,将使用缓存的 .pyc (位于同一目录中)文件代替 .py 文件,如果 .py 文件丢失。实际上,这并不常见,但是如果某些东西一直存在,那么考虑删除缓存文件是个好主意。当您尝试使用Python缓存系统或在不同环境中执行脚本时,这可能很重要。

Usually, the cache system works perfectly, but someday something may go wrong. It worth to note that the cached .pyc (lives in the same directory) file will be used instead of the .py file if the .py the file is missing. In practice, it's not a common occurrence, but if some stuff keeps up being "there", thinking about remove cache files is a good point. It may be important when you're experimenting with the cache system in Python or executing scripts in different environments.

您甚至不需要考虑它,但是缓存文件可以包含某种敏感信息。由于当前的实现,在 .pyc 文件中提供了实际文件的绝对路径。在某些情况下,您不想共享此类信息。

Most likely that you don't even need to think about it, but cache files can contain some sort of sensitive information. Due to the current implementation, in .pyc files presented an absolute path to the actual files. There are situations when you don't want to share such information.

似乎与字节码文件进行交互是相当不错的。经常性的需求,例如 django扩展具有适当的选项 compile_pyc clean_pyc

It seems that interacting with bytecode files is a quite frequent necessity, for example, django-extensions have appropriate options compile_pyc and clean_pyc.

这篇关于我应该将Python的pyc文件添加到.dockerignore吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆