简体   繁体   中英

AttributeError: 'module' object has no attribute 'new'

I have some code that I would like to run. Im new to python, as well as stackoverflow

Here is the python program:

# Circle Inversion Fractals (Apollonian Gasket) (Escape-time Algorithm)
# FB36 - 20131031
import math
import random
from collections import deque
from PIL import Image
imgx = 512; imgy = 512
image = Image.new("RGB", (imgx, imgy))
pixels = image.load()
n = random.randint(3, 6) # of main circles
a = math.pi * 2.0 / n
r = math.sin(a) / math.sin((math.pi - a) / 2.0) / 2.0 # r of main circles
h = math.sqrt(1.0 - r * r)
xa = -h; xb = h; ya = -h; yb = h # viewing area
cx = [0.0]; cy = [0.0]; cr = [1.0 - r] # center circle
for i in range(n): # add main circles
    cx.append(math.cos(a * i))
    cy.append(math.sin(a * i))
    cr.append(r)
maxIt = 64 # of iterations
for ky in range(imgy):
    print str(100 * ky / (imgy - 1)).zfill(3) + "%"
    for kx in range(imgx):
        x = float(kx) / (imgx - 1) * (xb - xa) + xa
        y = float(ky) / (imgy - 1) * (yb - ya) + ya
        queue = deque([])
        queue.append((x, y, 0))
        while len(queue) > 0: # iterate points until none left
            (x, y, i) = queue.popleft()
            for k in range(n + 1):
                dx = x - cx[k]; dy = y - cy[k]
                d = math.hypot(dx, dy)
                if d <= cr[k]:
                    dx = dx / d; dy = dy / d
                    dnew = cr[k] ** 2.0 / d
                    xnew = dnew * dx + cx[k]
                    ynew = dnew * dy + cy[k]
                    if xnew >= xa and xnew <= xb and ynew >= ya and ynew <= yb:
                        if i + 1 == maxIt: break
                        queue.append((xnew, ynew, i + 1))
        pixels[kx, ky] = (i % 16 * 16 , i % 8 * 32, i % 4 * 64)
image.save("CircleInversionFractal_" + str(n) + ".png", "PNG")

When I run it, I get an error message but I don't know how to resolve it.

 Traceback (most recent call last):
 File "U:\Personal\PythonFiles\Python Program Examples\Circle Inversion Fractals.py",   line 9, in <module>
 image = Image.new("RGB", (imgx, imgy))
 AttributeError: 'module' object has no attribute 'new'

What does this traceback mean?

It means that you are trying to use the name new on a module object:

>>> import sys
>>> sys.new
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'new'

It means the sys module has no attribute new here. It doesn't matter if you are treating new as a callable (function, method, class, etc.), in the expression sys.new() the name new will first be looked up as an attribute.

If you are trying to use a module that is documented to have a .new() method (such as sha.new() or hashlib.new() , then make sure you don't have another module by the same name in your path. Don't name your own script sha.py when you try to import the standard library module sha !

You can check where a module was imported from by printing the .__file__ filename:

 print sha.__file__

and if it is not a filename in your Python installation you perhaps need to investigate why you have an extra file in your path and rename that file or move it aside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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