我正在学习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
我有两个问题:
- 为什么
__pycache__
文件夹只创建file_regex.py
文件 - 我如何删除
__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文件夹。问题是文件与模块同名,所以我通过更改文件名来解决问题。