1

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.

thenorm
  • 35
  • 7
  • Are you running some kind of test before? Try running with `python -B` and see if this will resolve the issue. – Anzel Nov 06 '14 at 19:05

2 Answers2

4

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.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
0

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
Community
  • 1
  • 1
Anzel
  • 19,825
  • 5
  • 51
  • 52
  • I saw that answer, and running python with the -B flag, and though it prevents the __pycache__ folder from creating, the AttributeError still shows. Setting environment variable doesn't help either. – thenorm Nov 06 '14 at 19:39
  • are you within virtualenv? perhaps a reinstallation is required in your case – Anzel Nov 06 '14 at 19:41
  • what is virtualenv? Also python itself is not the issue, if I change to another directory everything is fine. Should I just delete the folder and retry everything? – thenorm Nov 06 '14 at 20:01
  • @thenorm, yea if change a directory will resolve this. I meant reinstall `json` modules if you are under virtualenv. virtualenv is a wrapper to separate your `pip` modules installtion from system wide installation – Anzel Nov 06 '14 at 20:04
  • I don't think I need to reinstall json modules. Anyway, I deleted the entire directory and rewrote everything, this seems to work. Any idea why this was happening? – thenorm Nov 06 '14 at 20:09
  • @thenorm, I am not entirely sure, but seems it has something to do with the `.pyc`. – Anzel Nov 06 '14 at 20:15
  • This is really strange, because deleting the .pyc didn't help either. Anyway, thanks for the help! – thenorm Nov 06 '14 at 22:19