博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
什么是__pycache__?
阅读量:2288 次
发布时间:2019-05-09

本文共 6292 字,大约阅读时间需要 20 分钟。

本文翻译自:

From what I understand, a cache is an encrypted file of similar files. 据我了解,缓存是类似文件的加密文件。

What do we do with the __pycache__ folder? 我们如何处理__pycache__文件夹? 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? 这个文件夹不断创建,它是做什么用的?


#1楼

参考:


#2楼

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. 当您在python中运行程序时,解释器首先将其编译为字节码(这是过分简化),并将其存储在__pycache__文件夹中。 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. 如果在其中查看,则会在项目文件夹中找到一堆共享.py文件名的文件,只有它们的扩展名是.pyc或.pyo。 These are bytecode-compiled and optimized bytecode-compiled versions of your program's files, respectively. 它们分别是程序文件的字节码编译版本和优化的字节码编译版本。

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 PYTHONDONTWRITEBYTECODE to any value (according to python's man page, any "non-empty string"). 如tcaswell所述,另一种选择是将环境变量PYTHONDONTWRITEBYTECODE设置为任何值(根据python的手册页,任何“非空字符串”)。


#3楼

__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. Python程序员通常忽略字节码。 Indeed __pycache__ and *.pyc are common lines to see in .gitignore files. 实际上__pycache__*.pyc.gitignore文件中常见的行。 Bytecode is not meant for distribution and can be disassembled using . 字节码不是要分发的,可以使用反汇编。


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 *.pyc for Python 2. 将Python 2的__pycache__替换为*.pyc

This sets a flag on all those directories (.pyc files) telling Finder/Textmate 2 to exclude them from listings. 这会在所有这些目录(.pyc文件)上设置一个标志,告诉Finder / Textmate 2将其从列表中排除。 Importantly the bytecode is there, it's just hidden. 重要的是字节码在那里,它只是隐藏的。

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 *.pyc files and __pycache__ folders is indeed useful. 运行单元测试是一种方案(在注释中更多),在该方案中,删除*.pyc文件和__pycache__文件夹确实很有用。 I use the following lines in my ~/.bash_profile and just run cl to clean up when needed. 我在~/.bash_profile使用以下行,并在需要时运行cl进行清理。

alias cpy='find . -name "__pycache__" -delete'alias cpc='find . -name "*.pyc"       -delete'...alias cl='cpy && cpc && ...'

#4楼

在3.2及更高版本中,Python将.pyc编译后的字节代码文件保存在名为__pycache__的子目录中,该子目录位于源文件所在的目录中,文件名带有标识创建它们的Python版本的文件名(例如script.cpython-33.pyc)。


#5楼

Updated answer from 3.7+ docs: 更新了3.7+文档中的答案:

To speed up loading modules, Python caches the compiled version of each module in the __pycache__ directory under the name module.version.pyc , where the version encodes the format of the compiled file; 为了加快模块的加载速度, Python将每个模块的编译版本都缓存在__pycache__目录下的名称module.version.pyc ,该版本对编译文件的格式进行编码; it generally contains the Python version number. 它通常包含Python版本号。 For example, in CPython release 3.3 the compiled version of spam.py would be cached as __pycache__/spam.cpython-33.pyc . 例如,在CPython版本3.3中,spam.py的编译版本将被缓存为__pycache__/spam.cpython-33.pyc This naming convention allows compiled modules from different releases and different versions of Python to coexist. 此命名约定允许来自不同发行版和不同版本的Python的编译模块共存。

Source: 来源: :

That is, this directory is generated by Python and exists to make your programs run faster. 也就是说,此目录由Python生成,并且存在以使您的程序运行更快。 It shouldn't be committed to source control, and should coexist in peace with your local source code. 它不应致力于源代码控制,而应与您的本地源代码和平共处。


__pycache__ is a directory that contains bytecode cache files that are automatically generated by python, namely compiled python, or .pyc , files. __pycache__是一个目录,其中包含由python自动生成的字节码缓存文件,即编译的python或.pyc文件。 You might be wondering why Python, an "interpreted" language, has any compiled files at all. 您可能想知道为什么Python(一种“解释”语言)根本没有任何编译文件。 addresses that (and it's definitely worth reading ). 解决了 (绝对值得阅读 )。

The python docs go into more depth about exactly how it works and why it exists: python文档更深入地介绍了它的确切工作方式及其存在的原因:

  • because the existing system of maintaining .pyc files in the same directory caused various problems, such as when a program was run with Python interpreters of different versions. 因为现有的在同一目录中维护.pyc文件的系统会引起各种问题,例如使用不同版本的Python解释器运行程序时。 For the full feature spec, see . 有关完整功能的规范,请参阅 。

#6楼

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 the program your second time to open the other file. 这使您第二次运行该程序以打开另一个文件时更快。

转载地址:http://micnb.baihongyu.com/

你可能感兴趣的文章
[Neo4j] CQL命令
查看>>
[Neo4j] 添加算法插件包
查看>>
[Neo4j] Spring Boot项目访问Neo4j报错
查看>>
[Docker] 两份docker-compose.xml共用一个network
查看>>
数据总线和地址总线的纠葛
查看>>
iar注释快捷键
查看>>
ubuntu16.04 okular中文界面
查看>>
Intel Core系列CPU架构演变
查看>>
Implement Queue using Stacks
查看>>
Power of Two
查看>>
Invert Binary Tree
查看>>
Implement Stack using Queues
查看>>
Maximal Square
查看>>
Single Number
查看>>
Best Time to Buy and Sell Stock II
查看>>
Different Ways to Add Parentheses
查看>>
Search a 2D Matrix II
查看>>
Product of Array Except Self
查看>>
Lowest Common Ancestor of a Binary Tree
查看>>
Kth Smallest Element in a BST
查看>>