Python3 项目删除 __pycache__ 文件夹和 .pyc 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28991015/
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
Python3 project remove __pycache__ folders and .pyc files
提问by SalientGreen
What is the BEST way to clear out all the __pycache__
folders and .pyc/.pyo
files from a python3 project. I have seen multiple users suggest the pyclean
script bundled with Debian, but this does not remove the folders. I want a simple way to clean up the project before pushing the files to my DVS.
从 python3 项目中清除所有__pycache__
文件夹和.pyc/.pyo
文件的最佳方法是什么。我已经看到多个用户建议使用pyclean
与 Debian 捆绑的脚本,但这并没有删除文件夹。我想要一种在将文件推送到我的 DVS 之前清理项目的简单方法。
采纳答案by SalientGreen
I found the answer myself when I mistyped pyclean as pycclean:
当我将 pyclean 错误输入为 pycclean 时,我自己找到了答案:
No command 'pycclean' found, did you mean:
Command 'py3clean' from package 'python3-minimal' (main)
Command 'pyclean' from package 'python-minimal' (main)
pycclean: command not found
Running py3clean .
cleaned it up very nicely.
跑步py3clean .
很好地清理了它。
回答by V. Gamula
You can do it manually with the next command:
您可以使用下一个命令手动执行此操作:
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
This will remove all *.pycfiles and __pycache__directories recursively in the current directory.
这将递归删除当前目录中的所有*.pyc文件和__pycache__目录。
回答by Aaron Hall
macOS & Linux
macOS 和 Linux
BSD's find
implementation on macOS is different from GNU find - this is compatible with both BSD and GNU find. Start with a globbing implementation, using -name
and the -o
for or- Put this function in your .bashrc
file:
BSDfind
在 macOS 上的实现与 GNU find 不同 - 这与 BSD 和 GNU find 兼容。开始一个通配符的实施,使用-name
和-o
对或-将这个功能在你的.bashrc
文件:
pyclean () {
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete
}
Then cd
to the directory you want to recursively clean, and type pyclean
.
然后cd
到您要递归清理的目录,然后键入pyclean
.
GNU find-only
GNU 仅查找
This is a GNU find, only (i.e. Linux) solution, but I feel it's a little nicer with the regex:
这是一个 GNU find,只有(即 Linux)解决方案,但我觉得使用正则表达式更好一点:
pyclean () {
find . -regex '^.*\(__pycache__\|\.py[co]\)$' -delete
}
Any platform, using Python 3
任何平台,使用 Python 3
On Windows, you probably don't even have find
. You do, however, probably have Python 3, which starting in 3.4 has the convenient pathlib
module:
在 Windows 上,您可能甚至没有find
. 但是,您可能有 Python 3,它从 3.4 开始具有方便的pathlib
模块:
python3 -Bc "import pathlib; [p.unlink() for p in pathlib.Path('.').rglob('*.py[co]')]"
python3 -Bc "import pathlib; [p.rmdir() for p in pathlib.Path('.').rglob('__pycache__')]"
The -B
flag tells Python not to write .pyc
files. (See also the PYTHONDONTWRITEBYTECODE
environment variable.)
该-B
标志告诉 Python 不要写.pyc
文件。(另请参阅PYTHONDONTWRITEBYTECODE
环境变量。)
The above abuses list comprehensions for looping, but when using python -c
, style is rather a secondary concern. Alternatively we could abuse (for example) __import__
:
上面的滥用列出了对循环的理解,但是在使用 时python -c
,样式是次要的问题。或者我们可以滥用(例如)__import__
:
python3 -Bc "for p in __import__('pathlib').Path('.').rglob('*.py[co]'): p.unlink()"
python3 -Bc "for p in __import__('pathlib').Path('.').rglob('__pycache__'): p.rmdir()"
Critique of an answer
对答案的批评
The top answer used to say:
最佳答案曾经说过:
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
This would seem to be less efficient because it uses three processes. find
takes a regular expression, so we don't need a separate invocation of grep
. Similarly, it has -delete
, so we don't need a separate invocation of rm
—and contrary to a comment here, it willdelete non-empty directories so long as they get emptied by virtue of the regular expression match.
这似乎效率较低,因为它使用三个进程。find
采用正则表达式,因此我们不需要单独调用grep
. 类似地,它有-delete
,所以我们不需要单独调用rm
— 并且与这里的注释相反,它会删除非空目录,只要它们由于正则表达式匹配而被清空。
From the xargs
man page:
从xargs
手册页:
find /tmp -depth -name core -type f -delete
Find files named core in or below the directory /tmp and delete them, but more efficiently than in the previous example (because we avoid the need to use fork(2) and exec(2) to launch rm and we don't need the extra xargs process).
find /tmp -depth -name core -type f -delete
在目录 /tmp 中或下面找到名为 core 的文件并删除它们,但比前面的示例更有效(因为我们避免了使用 fork(2) 和 exec(2) 来启动 rm 的需要,而且我们不需要额外的 xargs 进程)。
回答by ivan_pozdeev
Since this is a Python 3 project, you only need to delete __pycache__
directories -- all .pyc
/.pyo
files are inside them.
由于这是一个 Python 3 项目,您只需要删除__pycache__
目录——所有.pyc
/.pyo
文件都在其中。
find . -type d -name __pycache__ -exec rm -r {} \+
or its simpler form,
或者更简单的形式,
find . -type d -name __pycache__ -delete
which didn't work for me for some reason (files were deleted but directories weren't), so I'm including both for the sake of completeness.
由于某种原因,这对我不起作用(文件被删除但目录没有),所以为了完整起见,我将两者都包括在内。
Alternatively, if you're doing this in a directory that's under revision control, you can tell the RCS to ignore __pycache__
folders recursively. Then, at the required moment, just clean up all the ignored files. This will likely be more convenient because there'll probably be more to clean up than just __pycache__
.
或者,如果您在修订控制下的目录中执行此操作,您可以告诉 RCS__pycache__
递归忽略文件夹。然后,在需要的时候,只需清理所有被忽略的文件。这可能会更方便,因为可能要清理的不仅仅是__pycache__
.
回答by Bachsau
Why not just use rm -rf __pycache__
? Run git add -A
afterwards to remove them from your repository and add __pycache__/
to your .gitignore file.
为什么不直接使用rm -rf __pycache__
?git add -A
之后运行以将它们从您的存储库中删除并添加__pycache__/
到您的 .gitignore 文件中。
回答by Carlos Bazaga
This is my alias that works both with Python 2 and Python 3 removing all .pyc .pyo
files as well __pycache__
directories recursively.
这是我的别名,适用于 Python 2 和 Python 3,.pyc .pyo
以__pycache__
递归方式删除所有文件和目录。
alias pyclean='find . -name "*.py[co]" -o -name __pycache__ -exec rm -rf {} +'
回答by Craig
Thanks a lot for the other answers, based on them this is what I used for my Debian package's prerm
file:
非常感谢其他答案,基于它们,这是我用于 Debian 软件包prerm
文件的内容:
#!/bin/sh
set -e
deb_package='package-name'
python_package='package_name'
if which pyclean >/dev/null 2>&1; then
py3clean -p $deb_package
else
dpkg -L $deb_package | grep ${python_package}$ | while read file
do
find ${file} -type d -name __pycache__ -exec rm -r {} \+
done
fi
回答by AB Abhi
Using PyCharm
使用 PyCharm
To remove Python compiled files
删除 Python 编译文件
In the
Project Tool Window
, right-click a project or directory, where Python compiled files should be deleted from.On the context menu, choose
Clean Python compiled files
.
在 中
Project Tool Window
,右键单击应从中删除 Python 编译文件的项目或目录。在上下文菜单上,选择
Clean Python compiled files
。
The .pyc
files residing in the selected directory are silently deleted.
.pyc
驻留在选定目录中的文件将被静默删除。
回答by Rotareti
If you need a permanent solutionfor keeping Python cache files out of your project directories:
如果您需要一个永久的解决方案来将 Python 缓存文件保留在您的项目目录之外:
Starting with Python 3.8you can use the environment variable PYTHONPYCACHEPREFIX
to define a cache directory for Python.
从Python 3.8开始,您可以使用环境变量PYTHONPYCACHEPREFIX
为 Python 定义缓存目录。
From the Python docs:
来自 Python 文档:
If this is set, Python will write .pyc files in a mirror directory tree at this path, instead of in pycachedirectories within the source tree. This is equivalent to specifying the -X pycache_prefix=PATH option.
如果设置,Python 将在此路径的镜像目录树中写入 .pyc 文件,而不是在源树中的pycache目录中。这等效于指定 -X pycache_prefix=PATH 选项。
Example
例子
If you add the following line to your ./profile
in Linux:
如果您./profile
在 Linux 中添加以下行:
export PYTHONPYCACHEPREFIX="$HOME/.cache/cpython/"
Python won't create the annoying __pycache__
directories in your project directory, instead it will put all of them under ~/.cache/cpython/
Python 不会__pycache__
在您的项目目录中创建烦人的目录,而是会将它们全部放在~/.cache/cpython/
回答by Avi Gabrielle Pansensoy
Please just go to your terminal then type:
请前往您的终端,然后输入:
$rm __pycache__
and it will be removed.
它将被删除。