[英]Can you "nest" modules in python?
I'm working with a pygame tutorial and I'm importing the pygame module on a separate file from my main.py.我正在使用 pygame 教程,我正在将 pygame 模块导入到我的 main.py 的单独文件中。
pygame.py: pygame.py:
import pygame
class Game:
def __init__(self):
main.py:主要.py:
from game import Game
def main():
I'm guessing I already have my answer, since I keep receiving the error: "NameError: name 'pygame' is not defined", but is there any way to nest modules without importing them into your main?我猜我已经有了答案,因为我不断收到错误消息:“NameError: name 'pygame' is not defined”,但是有没有办法嵌套模块而不将它们导入你的主模块? Not a big deal, just thought I'd ask.没什么大不了的,只是想问问。
You might access imported modules inside imported modules following way您可以按照以下方式访问导入模块中的导入模块
mymodule.py我的模块.py
import math
class Math:
pass
main.py主文件
import mymodule
print(mymodule.math.cos(0.0))
then running python main.py
output然后运行python main.py
output
1.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.