简体   繁体   English

在Python 3中导入模块时出现AttributeError

[英]AttributeError when importing modules in Python 3

I am trying to write a script that makes use of the json and requests modules. 我正在尝试编写一个使用json和request模块的脚本。 Before I wrote the script, I was playing around with commands on the interactive shell, and since creating an actual file for my code, everything has somehow broken. 在编写脚本之前,我一直在交互式外壳上使用命令,并且自从为代码创建实际文件以来,所有内容都以某种方式破坏了。 The first time I ran the code, a pycache folder appeared in the folder and I think that is somehow breaking everthing. 第一次运行代码时,该文件夹中出现了一个pycache文件夹,我认为这一切都打破了。 The code, when run line by line in the shell, no longer works either with the presence of this pycache folder. 在外壳中逐行运行时,该代码在存在此pycache文件夹的情况下也不再起作用。 My code is as follows: 我的代码如下:

import json
import requests
r = requests.get('http://api.wunderground.com/api/78c2f37e6d924b1b/hourly/q/CA/Berkeley.json')
data = json.loads(r.text)
for x in range(0, 35):
    print(data['hourly_forecast'][x]['FCTTIME']['hour'])

This should print out all the hours in the weather forecast, but I get an "AttributeError: 'module' object has no attribute 'dumps'. In this folder, I also previously had another program that used external modules that also no long works with the presence of the pycache folder, so I am almost certain that it is causing the problems. However, deleting it doesn't fix anything as the code still doesn't work, and it just gets recreated. 这应该会打印出天气预报中的所有时间,但是我得到一个“ AttributeError:'module'对象没有属性'dumps'。在此文件夹中,我以前还拥有另一个使用外部模块的程序,该程序也不再与pycache文件夹的存在,所以我几乎可以肯定这是造成问题的原因,但是,删除它并不能解决任何问题,因为代码仍然无法正常工作,并且只能重新创建它。

EDIT: The problem was solved by deleting the entire buggy directory and rewriting everything. 编辑:通过删除整个错误目录并重写所有内容,解决了该问题。

The most common reason for 'module' object has no attribute 'xxx' , where 'xxx' is an attribute that you 'know' 'module' does have, is this: your program is in a directory that has a 'module.py' that you have forgotten about for the modment. 'module' object has no attribute 'xxx'的最常见原因是'module' object has no attribute 'xxx' xxx”是您“知道”模块具有的属性,原因是:您的程序位于具有“ module.py”的目录中'您已经忘记了这种方式。 So import module imports your module instead of the intended module in the stdlib (or elsewhere). 因此, import module导入您的模块,而不是stdlib(或其他地方)中的预期模块。 There have been multiple examples of this problem posted on python-list. 在python-list上发布了多个有关此问题的示例。 At least two were due to a forgotten-about random.py in the same directory. 至少有两个是由于在同一目录中被遗忘的random.py引起的。

The situation would have been clearer if you had posted the traceback. 如果您发布了追溯,情况将会更加清楚。

Please refer to this SO question What is pycache ? 请参阅此SO问题是什么pycache , see answer from @scott_fakename: ,请参阅@scott_fakename的答案:

When you run a program in python, the interpreter compiles it to bytecode first (this is an oversimplification) and stores it in the pycache folder. 当您在python中运行程序时,解释器会首先将其编译为字节码(这过于简化),并将其存储在pycache文件夹中。 If you look in there you will find a bunch of files sharing the names of the .py files in your project's folder, only their extentions will be either .pyc or .pyo. 如果您在其中查看,则会在项目文件夹中找到一堆共享.py文件名的文件,只有它们的扩展名是.pyc或.pyo。 These are bytecode-compiled and optimized bytecode-compiled versions of your program's files, respectively. 它们分别是程序文件的字节码编译版本和优化的字节码编译版本。

As a programmer, you can largely just ignore it... All it does is make your program start a little faster. 作为程序员,您基本上可以忽略它……它所做的只是使您的程序启动更快。 When your scripts change, they will be recompiled, and if you delete the files or the whole and run your program again, they will reappear (unless you specifically suppress that behavior) 当您的脚本更改时,它们将被重新编译,并且如果您删除文件或整个文件并再次运行程序,它们将重新出现(除非您明确禁止这种行为)

If you are using cpython (which is the most common, as it's the reference implementation) and you don't want that folder, then you can suppress it by starting the interpreter with the -B flag, for example 如果您使用的是cpython(这是最常见的实现,因为它是参考实现),并且您不希望使用该文件夹,则可以通过使用-B标志启动解释器来取消显示该文件夹。

python -B foo.py python -B foo.py

Another option, as noted by tcaswell, is to set the environment variable PYTHONDONTWRITEBYTECODE to any value (according to python's man page, any "non empty string"). 如tcaswell所述,另一种选择是将环境变量PYTHONDONTWRITEBYTECODE设置为任何值(根据python的手册页,任何“非空字符串”)。

So, you can either run: 因此,您可以运行:

python -B xxx.py

Or, set environment variable: 或者,设置环境变量:

PYTHONDONTWRITEBYTECODE = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM