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



我正在学习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__文件夹,其中.pyc文件用于file_regex.py文件。

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 zd{1,3}z[A-Z][a-z]+zW)�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),如果有帮助的话

这是因为__pycache__文件夹…

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

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

import file_regex

from file_regex import ...

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

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

最新更新