__pycache__ 文件夹每次执行 运行 文件夹中的任何其他文件

__pycache__ folder executes each time i run any other file in the folder

我正在学习 python 并且我有一个包含 5 或 6 个 python 文件的教程文件夹。其中之一包含正则表达式函数,比如 file_regex.py。问题是当我执行文件夹中的任何其他文件时,总是执行 file_regex.py,从而给出 file_regex.py 的输出。我没有在任何其他文件中导入 file_regex.py。

file_regex.py

import re

sentence = ''' Jessica_is_27 year7s old, whereas John is 12 but Stephanie is 12 and Marco is 50 years '''

ages = re.findall(r'\d{1,3}', sentence)

names = re.findall('[A-Z][a-z]+', sentence) test = re.findall('\W', sentence)

print(ages) 

print(names) 

print(test)

这是因为创建了 __pycache__ 文件夹,其中包含 file_regex.py 文件的 .pyc 文件。

regex.cpython-23.pyc

� Vo�U�@sjddlZdZejde�Zejde�Zejde�Zee�ee�ee�dS)�NzX Jessica_is_27 year7s old, whereas John is 12 but Stephanie is 12 and Marco is 50 years z\d{1,3}z[A-Z][a-z]+z\W)�re�sentence�findallZages�names�test�print�rr�!/home/sivasurya/tutorial/regex.py�<module>s

我有两个问题:

  1. 为什么 __pycache__ 文件夹只为 file_regex.py 文件创建
  2. 如何删除 __pycache__ 文件夹或此问题的解决方案(我尝试使用 python -B file1.py 命令编译 python 文件,但没有成功)

P.S:我在 miniconda 环境中工作 (python 3.x),如果有帮助的话

This is because of the __pycache__ folder . . .

这是不正确的。 __pycache__文件夹的存在与文件是否运行无关。它只包含编译后的文件,没有别的。

如果你的file_regex.py一直被执行是因为其他文件有

import file_regex

from file_regex import ...

其中。

我实际上将文件命名为 regex.py。当我删除 __pycache__ 文件夹并将文件名更改为 file_regex.py 时, __pycache__ 文件夹没有再次出现,问题就解决了。

在我的例子中,我的文件名为“timeit.py”,因此每次执行该文件时都会创建一个 pycache 文件夹。问题是该文件与模块同名,所以我通过更改文件名解决了问题。