2014-01-14 142 views
-1

我有一些我想运行的代码。 进出口新的蟒蛇,以及计算器AttributeError:'模块'对象没有属性'新'

这里是Python程序:

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

当我运行它,我得到一个错误消息,但我不知道如何解决它。

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' 

这个追溯是什么意思?

+0

如果您分享您尝试使用的代码,这将有助于提供更详细的答案。也许你正试图在Python中使用Ruby语法来实例化一个对象? –

+0

请分享您的代码,以便我们可以帮助您*更正错误。你正在尝试使用'hmac','sha','md5'还是'hashlib'模块? –

回答

2

这意味着你要使用的名字new模块对象上:

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

这意味着sys模块没有属性new这里。如果您将new作为可调用函数(函数,方法,类等)处理,则在sys.new()的表达式中,名称new将首先被查找为属性,这并不重要。

如果你正在尝试使用被证明有.new()方法的模块(如sha.new()hashlib.new(),然后确保你不要在你的路径相同的名称有另一个模块。不要命名自己的脚本sha.py当您尝试导入标准库模块sha

您可以检查其中一个模块从通过打印.__file__文件名输入:!

print sha.__file__ 

如果它是而不是您的Python安装中的文件名,您可能需要调查为什么您的路径中有一个额外的文件,并重命名该文件或将其移到一边。

+0

真棒..那行'当你尝试导入标准库模块时,不要命名你自己的脚本sha.py! – Deep

相关问题