英文:
Can't Run program beyond a certain point using modules
问题
在我正在开发的终端模拟器中,我有三个不同的模块,以及主程序。其中一个模块将文本打印得像正在输入一样,一个用于清除屏幕,另一个提供类似于ls和cd的简单终端命令。
我已经修改了终端命令模块,以便在每个字符出现在屏幕上之间有延迟,使用了前面提到的打字模块。它按照预期工作,但当我尝试将两个模块导入到主脚本中时,它似乎运行正常,直到调用终端命令模块,然后会引发未定义的函数错误。然后,如果在两者中都导入了模块,我会得到一个错误,指示打字模块中没有typingPrint属性,而实际上是有的。
我尝试删除终端命令模块中对打字模块的导入语句,然后会出现一个错误,指示typing未定义,但如果我尝试从主脚本中删除typing并重新添加导入语句到终端命令模块,我会得到相同的错误。如果主脚本和终端命令模块都导入了打字模块,那么会出现AttributeError错误,我曾以为这是循环导入错误,但实际上不是。
打字模块的导入语句:
import time, sys, colorama, random
from colorama import Fore, init, Back, Style
from os import system, name
终端命令(fileModification)的导入语句:
import os, shutil, typing
主脚本的导入语句:
import time, sys, shutil, random
from Lib import typing, clear, fileModification
编辑:如果我将整个脚本恢复为不使用延迟,它可以正常工作。我唯一得到的错误是打字模块中似乎没有属性的错误。
英文:
In my terminal emulator I'm working on, I have 3 different modules, as well as the main program. One that prints text as if it was being typed, one to clear the screen, and one that offers simple terminal commands like ls and cd.
I had modified the terminal commands module so that it would have the delay between each character appearing on the screen, using the aforementioned typing module. It worked as it was intended, but when I would try to import both module into the main script, It appears to run fine, up until the terminal commands module is called, where it throws a undefined function. Then, if in both, I would get an error stating there was no typingPrint attribute in the typing module, when there is.
I tried to remove the import statement for the typing module from the terminal command module, and I get an error stating that typing is not defined, but then if I try removing typing from the main script and re-adding the import statement to the terminal command module, I get the same thing
If both the main script and the terminal command module have the typing module imported, then an AttributeError, which I thought was a circular import error, but it wasnt.
Typing Module import statement:
import time, sys, colorama, random
from colorama import Fore, init, Back, Style
from os import system, name
Terminal command (fileModification) import statement:
import os, shutil, typing
Main script import statement:
import time, sys, shutil, random
from Lib import typing, clear, fileModification
Edit: If I revert the whole script to not use the delay, it works fine. The only error I was getting was that there was supposedly not an attribute for the typing module.
答案1
得分: 0
有两个问题,都与fileModification
模块的导入语句有关。
第一个问题是Python有一个名为typing
的内置模块。因此,在fileModification
模块中导入typing
时,Python会导入内置版本,而不是您的版本。
然而,第二个更深层次的问题是,即使您对您的typing
模块使用了一个非模棱两可的名称,导入仍然会失败,因为您没有指定正确的路径来获取您的自定义typing
模块。换句话说,您无法仅通过import typing
从fileModification
模块访问到Lib.typing
。
要解决这个问题,您可以将fileModification
模块的导入语句更改为
import os, shutil
from Lib import typing
但更好的做法是,我建议您将typing
模块的名称更改为不那么模棱两可的名称。
英文:
There are two issues, both related to the import statement of the fileModification
module.
The first issue is that Python has a built-in module called typing
. Hence, when you import typing
in the fileModification
module, Python imports the built-in version, not your version.
However, the second, deeper issue is that even if you had used a non-ambiguous name for your typing
module, the import would still fail because you did not specify the correct path to fetch your custom typing
module. In other words, you will not reach Lib.typing
from the fileModification
module just by import typing
.
To resolve the issue, you can change the import statements of your fileModification
module to
import os, shutil
from Lib import typing
But better yet, I'd recommend you change the name of your typing
module to something less ambiguous.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论