为什么说无法导入

问题描述 投票:0回答:1

我正在尝试将另一个文件 (tiles.py) 导入到我的主文件 (main.py) 中,但它有一些导入错误。我正在尝试将一个名为 Tile 的类导入到我的主类中。错误是

 Traceback (most recent call last):
 File "c:\Users\shireen\Desktop\Clear Code\main.py", line 3, in <module>
 from tiles import Tile
 ImportError: cannot import name 'Tile' from 'tiles' 
 (c:\Users\shireen\Desktop\ClearCode\tiles.py)

我的main.py文件代码:

import pygame, sys
from settings import *
from tiles import Tile

# Pygame setup
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
test_tile = pygame.sprite.Group(Tile((100, 100), 200))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill('black')
    test_tile.draw(screen)

    pygame.display.update()
    clock.tick(60)

我的tiles.py文件代码:

import pygame

class Tile(pygame.sprite.Sprite):
    def __init__(self,pos,size):
        super().__init__()
        self.image = pygame.Surface((size, size))
        self.image.fill('grey')
        self.rect = self.image.get_rect(topleft = pos)
python python-module
1个回答
0
投票

问题是由路径中的空格引起的。比较一下:

File "c:\Users\shireen\Desktop\Clear Code\main.py", line 3, in <module>

(c:\Users\shireen\Desktop\ClearCode\tiles.py)

您只需要将“Clear Code”重命名为“ClearCode”或smth

© www.soinside.com 2019 - 2024. All rights reserved.