什么是__pycache__? [英] What is __pycache__?

查看:107
本文介绍了什么是__pycache__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,缓存是类似文件的加密文件。

From what I understand, a cache is an encrypted file of similar files.

我们如何使用 __ pycache __ 文件夹?是我们提供给人们的,而不是我们提供的源代码吗?只是我的输入数据吗?这个文件夹不断创建,它是做什么用的?

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?

推荐答案

在python中运行程序时,解释器首先将其编译为字节码(这是一个过分的简化)并将其存储在 __ pycache __ 文件夹。如果您在其中查看,则会在您的项目文件夹中找到一堆共享.py文件名的文件,只有它们的扩展名是.pyc或.pyo。分别是程序文件的字节码编译版本和优化的字节码编译版本。

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.

作为一名程序员,您基本上可以忽略它……所有的事情就是使程序启动。快一点。当脚本更改时,将重新编译它们,并且如果删除文件或整个文件夹并再次运行程序,它们将重新出现(除非您明确禁止这种行为)。

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).

何时您将代码发送给其他人时,通常的做法是删除该文件夹,但是是否执行此操作并不重要。当您使用版本控制( git )时,此文件夹通常列在忽略文件( .gitignore )中

When you're sending your code to other people, the common practice is to delete that folder, but it doesn't really matter whether you do or don't. When you're using version control (git), this folder is typically listed in the ignore file (.gitignore) and thus not included.

如果您使用的是cpython(这是最常见的,因为它是参考实现),并且您不想要该文件夹,则可以通过启动它来抑制它具有-B标志的解释器,例如

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

python -B foo.py

tcaswell指出,另一种选择是将环境变量 PYTHONDONTWRITEBYTECODE 设置为任何值(根据python的手册页, 非空字符串)。

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").

这篇关于什么是__pycache__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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