如何循环.py文件python导入模块?

发布于 2025-02-04 06:11:36 字数 825 浏览 2 评论 0原文

我有一个程序,要求用户意图。他们可以决定上传,检索,重置或设置首选项。问题是当他们上传信息时,我需要两个py文件才能在循环中运行,直到满足需求为止。我看起来像这样的东西:

def MainTerminal():
    import User_Intent
    import ProceduresTerminal

    determine_intent = open(gvar.procedure_upload, 'r')
    if determine_intent.read() == '1':
        determine_intent.close()
        import Upload_Data
        import Data_Processing
 
        R_Check = open(gvar.RActivation, 'r')
        R_Check_Read = R_Check.read()
        R_Check.close()

        if R_Check_Read == '1':
            import C_Processing
            while R_Check_Read == '1':
                import R_Processing
                import C_Processing
        else:
            import C_Processing

如果函数起作用,则while循环无法正常工作。有其他方法可以做到吗?在while循环中,C_Processing应该在将R_CHECK_READ设置为'0'时结束循环,此部分正在工作,但while loop却行不通。

I have a program that asks the user for their intent. They can decide to either Upload, Retrieve, Reset, or Set Preferences. The problem is when they upload information I need two py files to run in a loop until the requirement is met. What I have looks like sorta like this:

def MainTerminal():
    import User_Intent
    import ProceduresTerminal

    determine_intent = open(gvar.procedure_upload, 'r')
    if determine_intent.read() == '1':
        determine_intent.close()
        import Upload_Data
        import Data_Processing
 
        R_Check = open(gvar.RActivation, 'r')
        R_Check_Read = R_Check.read()
        R_Check.close()

        if R_Check_Read == '1':
            import C_Processing
            while R_Check_Read == '1':
                import R_Processing
                import C_Processing
        else:
            import C_Processing

The if functions work however, the while loop does not function. Is there a different way to do this? In the While loop C_Processing is supposed to end the loop when it sets R_Check_Read to '0', this part is working but the while loop does not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2025-02-11 06:11:36

有其他方法可以这样做吗?

是的。

将所有导入语句放在文件顶部。

他们应该def一些功能。 循环中的中调用一对此类功能。


导入模块应大部分仅定义函数,
再加上一些变量。
避免print()语句或进口时间发生的其他副作用。
将这些操作推迟到运行时间,当调用者调用您的功能时。

了解导入结果被缓存,
因此,重复导入同一模块将无效。


使用 isort 管理您的导入。
(他们的座右铭?“我对您的进口进行排序,所以您不必这样做。”)

Is there a different way to do this?

Yes.

Put all your import statements at top-of-file.

They should def some functions. Call a pair of such functions within your while loop.


Importing a module should mostly just define functions,
plus maybe a few variables.
Avoid print() statements or other side effects that happen at import time.
Defer such actions until run time, when caller invokes your function.

Understand that import results are cached,
so repeated import of same module will have no effect.


Use isort to manage your imports.
(Their motto? "I sort your imports, so you don't have to.")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文