【问题标题】:__pycache__ folder executes each time i run any other file in the folder__pycache__ 文件夹在我每次运行文件夹中的任何其他文件时执行
【发布时间】:2015-08-31 15:46:42
【问题描述】:

我正在学习 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 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)中工作,如果有帮助的话

【问题讨论】:

  • __pycache__ 存储字节码的缓存副本。仅当您明确导入 file_regex.py 模块时,该模块的缓存字节码才会被加载。没有您明确请求,__pycache__ 中的任何内容都不会自动加载。
  • 但是,其余文件没有导入 file_regex.py 文件..
  • 依赖可以传递。使用 -v 开关运行 Python 以查看执行时的所有导入。

标签: python regex python-3.x memcached miniconda


【解决方案1】:

这是因为 __pycache__ 文件夹。 . .

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

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

import file_regex

from file_regex import ...

在其中。

【讨论】:

  • 我只导入以下模块:'import nltk from nltk.corpus import state_union from nltk.tokenize import PunktSentenceTokenizer' 它无处导入 file_regex 模块。
  • 您认为它正在被导入是怎么回事?请用上述评论更新您的问题,以及此问题的答案。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2011-12-09
      • 2016-05-12
      • 2019-05-11
      相关资源
      最近更新 更多