Python奇怪的NameError

时间:2015-04-24 08:22:07

标签: python python-3.4

我有一个NameError,我似乎无法找到原因。错误跟踪就像这样结束。

MapView()


new_map.py位于文件from common import * 中,该文件在导入中包含此行:

common.py


文件def reset_screen(): # check if display has been initialized if not pygame.display.get_init(): pygame.init() # set screen #flags = pygame.FULLSCREEN | pygame.DOUBLEBUF screen = pygame.display.set_mode( options.window_size ) # fill with black screen.fill(BLACK) return screen 中包含以下功能。

new_main.py

我错过了什么?

E:这是我对每个文件的导入。

from common import * from new_map import Map, MapView import pygame, sys

common.py

import pygame import options from config_reader import ConfigReader from constants import * from coordinates import Coordinates

config_reader.py

from action import Action from new_map import Map from object_type import ObjectType from squaretype import SquareType from new_character import Character from coordinates import Coordinates import direction

new_map.py

from square import Square from common import * from constants import * from coordinates import Coordinates from map_object import MapObject from object_type import ObjectType from new_character import Character from turn import TurnController import pygame, os

    public void PurgeRotation(string src, string dest)
    {
        float iPages, iPageCntr;
        Doc theSrc = new Doc();

        theSrc.Read(src);

        double iSourceX, iSourceY;
        iSourceX = theSrc.MediaBox.Width;
        iSourceY = theSrc.MediaBox.Height;
        iPages = theSrc.PageCount;


        Doc theDst = new Doc();
        theDst.MediaBox.Width = theSrc.MediaBox.Height;
        theDst.MediaBox.Height = theSrc.MediaBox.Width;

        double h = theSrc.MediaBox.Height;
        double w = theSrc.MediaBox.Width;
        //theDst.Rect.Rectangle=theSrc.MediaBox;(Not working);
        theDst.Rect.String = theSrc.MediaBox.ToString();

        XRect rect = new XRect();


        theDst.Transform.Rotate(270, 0, 0);
        theDst.Transform.Translate(0, iSourceX);

        for (iPageCntr = 1; iPageCntr <= iPages; iPageCntr++)
        {
            theDst.Page = theDst.AddPage();
            theDst.AddImageDoc(theSrc,(int) iPageCntr, null);

        }

        // Save new A3 format
        theDst.Save(dest);

        theSrc.Clear();
        theDst.Clear();



    }

2 个答案:

答案 0 :(得分:1)

查看您的跟踪我还可以在您的common.py中找出包含new_map.py的内容,这会导致new_map中的导入失败。如果我对我的测试文件这样做,我会得到同样的错误:

我的主要 - 导入两者但仍然不会导致循环引用:

if __name__ == "__main__":
    from common import *  # <-- this is ok
    from new_map import * # <-- this is no problem 
    do_something()
    test()

我的公地:

from new_map import *    # <--- this will cause the problem

def reset_screen():
    return 1

def do_something():
    return test()

我的new_map:

from common import *    # <--- this will fail then

def test():
    return reset_screen()

因此,您必须使用new_map将common.py拆分为部分,而不必使用另一个部分,然后在new_map中导入包含函数的版本,而不包含new_map。

common_map.py

from new_map import *

def do_something()
    return test()

common_nomap.py

def reset_screen()
    return 1

new_map.py:

from common_nomap import *

def test()
    return reset_screen()

main.py

if __name__ == "__main__":
    from common_map import *  # <-- this is ok
    from new_map import *     # <-- this is no problem 
    do_something()
    test()

答案 1 :(得分:0)

我能想到的两种可能情景:

  1. 循环代码: common.py 似乎激活 new_map.py 中的功能(通过 config_reader.py ),这可能导致 new_map.py 不导入 common.py

  2. 您可能导入的常用包而不是 common.py