导入 python 脚本无效

Importing python scripts not working

我正在尝试将 python 脚本 (graphics.py) 导入到我的其他脚本 (game.py) 中,但是它不起作用。

在game.py中:

import pygame
import graphics.py
import sys # Mainly for sys.quit

"""********Start variables********"""

# Some colour constants
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
GREEN = (  0, 255,   0)
BLUE  = (  0,   0, 255)

# Display settings
FPS = 60
fpsClock = pygame.time.Clock()


def main():
    pygame.init()

    # Load graphics
    gfx = graphics()

在graphics.py中:

import pygame

class graphics(object):

    # Create a surface that will hold the player sprite
    SprPlayer = pygame.Surface((32, 32))

    # At the beggining of the object, load the sprites
    def __init__():
        SprPlayer = pygame.image.load("Graphics\Player.png")

所以我正在制作我的第一个 python 游戏,(Python 是除了 GML 之外我最喜欢的新语言。)而且我不能将所有内容都放在一个文件中。 这是错误

import graphics.py
ImportError: No module named py

我在同一个目录中有两个脚本,所以我不知道发生了什么。非常感谢任何帮助。

在 Python 中,当您尝试导入 python 模块时不需要包含文件扩展名,因此 No module named py 因为 python 正在尝试导入从名为 graphics 的文件夹导入名为 py 的文件。你想要的只是 import graphics.

https://docs.python.org/2/tutorial/modules.html

在python中,您不需要在同一目录中的文件末尾添加“.py”扩展名。尝试 import graphics。要了解更多信息,请查看 this Whosebug 答案。