AttributeError when importing modules in Python 3

喜欢而已 提交于 2020-01-14 04:40:51

问题


I am trying to write a script that makes use of the json and requests modules. 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. The code, when run line by line in the shell, no longer works either with the presence of this pycache folder. 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.

EDIT: The problem was solved by deleting the entire buggy directory and rewriting everything.


回答1:


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. So import module imports your module instead of the intended module in the stdlib (or elsewhere). There have been multiple examples of this problem posted on python-list. At least two were due to a forgotten-about random.py in the same directory.

The situation would have been clearer if you had posted the traceback.




回答2:


Please refer to this SO question What is pycache?, see answer from @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. 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. 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

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").

So, you can either run:

python -B xxx.py

Or, set environment variable:

PYTHONDONTWRITEBYTECODE = 1


来源:https://stackoverflow.com/questions/26787309/attributeerror-when-importing-modules-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!