Python 什么是 __pycache__?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16869024/
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
What is __pycache__?
提问by user2063042
From what I understand, a cache is an encrypted file of similar files.
据我了解,缓存是类似文件的加密文件。
What do we do with the __pycache__folder? Is it what we give to people instead of our source code? Is it just my input data? This folder keeps getting created, what it is for?
我们用__pycache__文件夹做什么?它是我们提供给人们的东西而不是我们的源代码吗?这只是我的输入数据吗?这个文件夹一直在创建,它是做什么用的?
回答by scott_fakename
When you run a program in python, the interpreter compiles it to bytecode first (this is an oversimplification) and stores it in the __pycache__folder. If you look in there you will find a bunch of files sharing the names of the .py files in your project's folder, only their extensions will be either .pyc or .pyo. These are bytecode-compiled and optimized bytecode-compiled versions of your program's files, respectively.
当您在 python 中运行程序时,解释器首先将其编译为字节码(这是一种过度简化)并将其存储在__pycache__文件夹中。如果您查看那里,您会发现一堆共享项目文件夹中 .py 文件名称的文件,只有它们的扩展名是 .pyc 或 .pyo。这些分别是程序文件的字节码编译版本和优化字节码编译版本。
As a programmer, you can largely just ignore it... All it does is make your program start a little faster. When your scripts change, they will be recompiled, and if you delete the files or the whole folder and run your program again, they will reappear (unless you specifically suppress that behavior)
作为程序员,您基本上可以忽略它……它所做的只是让您的程序启动得更快一点。当您的脚本更改时,它们将被重新编译,如果您删除文件或整个文件夹并再次运行您的程序,它们将重新出现(除非您明确禁止该行为)
If you are using cpython (which is the most common, as it's the reference implementation) and you don't want that folder, then you can suppress it by starting the interpreter with the -B flag, for example
如果您使用的是 cpython(这是最常见的,因为它是参考实现)并且您不想要该文件夹,那么您可以通过使用 -B 标志启动解释器来抑制它,例如
python -B foo.py
Another option, as noted by tcaswell, is to set the environment variable PYTHONDONTWRITEBYTECODEto any value (according to python's man page, any "non-empty string").
正如 tcaswell 所指出的,另一种选择是将环境变量设置PYTHONDONTWRITEBYTECODE为任何值(根据 python 的手册页,任何“非空字符串”)。
回答by line break
__pycache__is a folder containing Python 3 bytecode compiled and ready to be executed.
__pycache__是一个包含Python 3 字节码编译并准备执行的文件夹。
I don't recommend routinely deleting these files or suppressing creation during development as it may hurt performance. Just have a recursive command ready (see below) to clean up when needed as bytecode can become stale in edge cases (see comments).
我不建议在开发过程中定期删除这些文件或禁止创建,因为这可能会影响性能。只需准备好递归命令(见下文)即可在需要时进行清理,因为字节码在边缘情况下可能会变得陈旧(见评论)。
Python programmers usually ignore bytecode. Indeed __pycache__and *.pycare common lines to see in .gitignorefiles. Bytecode is not meant for distribution and can be disassembled using dismodule.
Python 程序员通常会忽略字节码。确实__pycache__和*.pyc是.gitignore文件中常见的行。字节码不用于分发,可以使用dismodule进行反汇编。
If you are using OS X you can easily hide all of these folders in your project by running following command from the root folder of your project.
如果您使用的是 OS X,您可以通过从项目的根文件夹运行以下命令轻松隐藏项目中的所有这些文件夹。
find . -name '__pycache__' -exec chflags hidden {} \;
Replace __pycache__with *.pycfor Python 2.
更换__pycache__用*.pyc的Python 2。
This sets a flag on all those directories (.pyc files) telling Finder/Textmate 2 to exclude them from listings. Importantly the bytecode is there, it's just hidden.
这会在所有这些目录(.pyc 文件)上设置一个标志,告诉 Finder/Textmate 2 从列表中排除它们。重要的是字节码在那里,它只是隐藏的。
Rerun the command if you create new modules and wish to hide new bytecode or if you delete the hidden bytecode files.
如果您创建新模块并希望隐藏新字节码或删除隐藏的字节码文件,请重新运行该命令。
On Windows the equivalent command might be (not tested, batch script welcome):
在 Windows 上,等效命令可能是(未测试,欢迎使用批处理脚本):
dir * /s/b | findstr __pycache__ | attrib +h +s +r
Which is same as going through the project hiding folders using right-click > hide...
这与使用右键单击>隐藏...浏览项目隐藏文件夹相同。
Running unit tests is one scenario (more in comments) where deleting the *.pycfiles and __pycache__folders is indeed useful. I use the following lines in my ~/.bash_profileand just run clto clean up when needed.
运行单元测试是删除*.pyc文件和__pycache__文件夹确实有用的一种场景(更多在评论中)。我在我的中使用以下几行,~/.bash_profile并cl在需要时运行以进行清理。
alias cpy='find . -name "__pycache__" -delete'
alias cpc='find . -name "*.pyc" -delete'
...
alias cl='cpy && cpc && ...'
回答by Shashidhar Yalagi
In 3.2 and later, Python saves .pyc compiled byte code files in a sub-directory named __pycache__located in the directory where your source files reside with filenames that identify the Python version that created them (e.g. script.cpython-33.pyc)
在 3.2 及更高版本中,Python 将 .pyc 编译的字节码文件保存在名为__pycache__的子目录中,该子目录位于源文件所在的目录中,文件名标识创建它们的 Python 版本(例如 script.cpython-33.pyc)
回答by FluxLemur
Updated answer from 3.7+ docs:
来自 3.7+ 文档的更新答案:
To speed up loading modules, Python caches the compiled version of each module in the
__pycache__directoryunder the namemodule.version.pyc, where the version encodes the format of the compiled file; it generally contains the Python version number. For example, in CPython release 3.3 the compiled version of spam.py would be cached as__pycache__/spam.cpython-33.pyc. This naming convention allows compiled modules from different releases and different versions of Python to coexist.
为了加快加载模块的速度,Python 将每个模块的编译版本缓存在
__pycache__name 下的 目录中module.version.pyc,其中版本对编译文件的格式进行编码;它通常包含 Python 版本号。例如,在 CPython 3.3 版中,spam.py 的编译版本将被缓存为__pycache__/spam.cpython-33.pyc. 这种命名约定允许来自不同版本和不同 Python 版本的编译模块共存。
Source: https://docs.python.org/3/tutorial/modules.html#compiled-python-files
来源:https: //docs.python.org/3/tutorial/modules.html#compiled-python-files
That is, this directory is generated by Python and exists to make your programs run faster. It shouldn't be committed to source control, and should coexist in peace with your local source code.
也就是说,这个目录是由 Python 生成的,它的存在是为了让你的程序运行得更快。它不应该致力于源代码控制,而应该与您的本地源代码和平共处。
__pycache__is a directory that contains bytecode cache files that are automatically generated by python, namely compiled python, or .pyc, files. You might be wondering why Python, an "interpreted" language, has any compiled files at all. This SO questionaddresses that (and it's definitely worth reading this answer).
__pycache__是一个包含python自动生成的字节码缓存文件的目录,即编译后的python,或者.pyc,文件。您可能想知道为什么 Python 是一种“解释型”语言,为什么会有任何编译文件。这个 SO 问题解决了这个问题(并且绝对值得阅读这个答案)。
The python docs go into more depth about exactly how it works and why it exists:
python 文档更深入地了解它的工作原理及其存在的原因:
- It was added in python 3.2because the existing system of maintaining
.pycfiles in the same directory caused various problems, such as when a program was run with Python interpreters of different versions. For the full feature spec, see PEP 3174.
- 它是在python 3.2中添加的,因为现有的将
.pyc文件维护在同一目录中的系统会导致各种问题,例如当程序运行时使用不同版本的Python解释器。有关完整功能规范,请参阅PEP 3174。
回答by Aleks. S.
A __pycache__folder is created when you use the line:
__pycache__使用以下行时会创建一个文件夹:
import file_name
or try to get information from another file you have created. This makes it a little faster when running your program the second time to open the other file.
或尝试从您创建的另一个文件中获取信息。这使得第二次运行程序打开另一个文件时速度会更快一些。
回答by Marcus Thornton
The python interpreter compiles the *.py script file and saves the results of the compilation to the __pycache__directory.
python解释器编译*.py脚本文件,并将编译结果保存到__pycache__目录中。
When the project is executed again, if the interpreter identifies that the *.py script has not been modified, it skips the compile step and runs the previously generated *.pyc file stored in the __pycache__folder.
当项目再次执行时,如果解释器识别出 *.py 脚本没有被修改,它会跳过编译步骤并运行存储在__pycache__文件夹中的先前生成的 *.pyc 文件。
When the project is complex, you can make the preparation time before the project is run shorter. If the program is too small, you can ignore that by using python -B abc.pywith the Boption.
当项目复杂时,可以缩短项目运行前的准备时间。如果程序太小,您可以通过python -B abc.py与B选项一起使用来忽略它。
回答by Yossarian42
from the official python tutorial Modules
来自官方python教程模块
To speed up loading modules, Python caches the compiled version of each module in the
__pycache__directory under the namemodule.version.pyc, where the version encodes the format of the compiled file; it generally contains the Python version number. For example, in CPython release 3.6 the compiled version of spam.py would be cached as__pycache__/spam.cpython-36.pyc.
为了加快加载模块的速度,Python 将每个模块的编译版本缓存在
__pycache__name 下的目录中module.version.pyc,其中版本对编译文件的格式进行编码;它通常包含 Python 版本号。例如,在 CPython 3.6 版中,spam.py 的编译版本将被缓存为__pycache__/spam.cpython-36.pyc.
from Python doc Programming FAQs
来自 Python 文档编程常见问题解答
When a module is imported for the first time (or when the source file has changed since the current compiled file was created) a .pyc file containing the compiled code should be created in a
__pycache__subdirectory of the directory containing the.pyfile. The.pycfile will have a filename that starts with the same name as the.pyfile, and ends with.pyc, with a middle component that depends on the particular python binary that created it.
当模块第一次被导入时(或者当源文件自创建当前编译文件以来发生了变化)时,应该在
__pycache__包含该.py文件的目录的子目录中创建一个包含编译代码的 .pyc文件。该.pyc文件将有一个文件名,该.py文件名以与文件相同的名称开头,以, 结尾.pyc,中间组件取决于创建它的特定 python 二进制文件。
回答by TechFree
Execution of a python script would cause the byte code to be generated in memory and kept until the program is shutdown. In case a module is imported, for faster reusability, Python would create a cache .pyc (PYC is 'Python' 'Compiled') file where the byte code of the module being imported is cached. Idea is to speed up loading of python modules by avoiding re-compilation ( compile once, run multiple times policy ) when they are re-imported.
执行 python 脚本会导致字节码在内存中生成并保留到程序关闭。在导入模块的情况下,为了更快的可重用性,Python 将创建一个缓存 .pyc(PYC 是 'Python' 'Compiled')文件,其中缓存了正在导入的模块的字节码。想法是通过在重新导入时避免重新编译(编译一次,多次运行策略)来加快python模块的加载。
The name of the file is the same as the module name. The part after the initial dot indicates Python implementation that created the cache (could be CPython) followed by its version number.
文件名与模块名相同。初始点后面的部分表示创建缓存的 Python 实现(可能是 CPython),后跟其版本号。
回答by Alok Tiwari
Python Version 2.x will have .pycwhen interpreter compiles the code.
当解释器编译代码时,Python 版本 2.x 将具有.pyc。
Python Version 3.x will have __pycache__when interpreter compiles the code.
当解释器编译代码时,Python 版本 3.x 将具有__pycache__。
alok@alok:~$ ls
module.py module.pyc __pycache__ test.py
alok@alok:~$
回答by Ehsan Barkhordar
When you import a module,
当您导入模块,
import file_name
Python stores the compiled bytecode in __pycache__directory so that future imports can use it directly, rather than having to parse and compile the source again.
Python 将编译后的字节码存储在__pycache__目录中,以便将来的导入可以直接使用它,而不必再次解析和编译源代码。
It does not do that for merely running a script, only when a file is imported.
它不会仅在运行脚本时执行此操作,仅在导入文件时执行此操作。
(Previous versions used to store the cached bytecode as .pyc files that littered up the same directory as the .py files, but starting in Python 3 they were moved to a subdirectory to make things tidier.)
(以前的版本用于将缓存的字节码存储为 .pyc 文件,这些文件散落在与 .py 文件相同的目录中,但从 Python 3 开始,它们被移动到一个子目录以使事情更整洁。)
PYTHONDONTWRITEBYTECODE---> If this is set to a non-empty string, Python won't try to write .pyc files on the import of source modules. This is equivalent to specifying the -B option.
PYTHONDONTWRITEBYTECODE---> 如果将其设置为非空字符串,Python 将不会尝试在导入源模块时写入 .pyc 文件。这等效于指定 -B 选项。

