简体   繁体   English

导入 Python 出现问题

[英]Trouble with importing in Python

I'm trying to create a module that requires variables from the main class, so I imported said variables to the module, but when I try to test my new module by importing it into the main class, it says it can't import it.我正在尝试创建一个需要来自主 class 的变量的模块,因此我将所述变量导入到模块中,但是当我尝试通过将其导入主 class 来测试我的新模块时,它说它无法导入它.

It seems to be BECAUSE I'm importing the main class in the new module that causes the issue because whenever I remove the import, it works but it can no longer access the variables from the main class needed to function.似乎是因为我在导致问题的新模块中导入了主 class,因为每当我删除导入时,它都可以工作,但它无法再访问 ZC1C425268E68385D1AB5074F 所需的主 class 中的变量。

The main class:主要class:

from Mod import Mod

variable1=5
variable2=3

mod=Mod()

mod.task()

The new module:新模块:

from Main import variable1, variable2

class Mod:
    def task(self):
        print(variable1+variable2)

When I run the main class I get this:当我运行主 class 时,我得到了这个:

Traceback (most recent call last):
  File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Main.py", line 1, in <module>
    from Mod import Mod
  File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Mod.py", line 1, in <module>
    from Main import variable1, variable2
  File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Main.py", line 1, in <module>
    from Mod import Mod
ImportError: cannot import name 'Mod' from 'Mod' (D:\.here\Computer Science\Computer Science Stuff\Python Projects\Mod.py)

And when I run the new module I get this:当我运行新模块时,我得到了这个:

Traceback (most recent call last):
  File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Mod.py", line 1, in <module>
    from Main import variable1, variable2
  File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Main.py", line 1, in <module>
    from Mod import Mod
  File "D:\.here\Computer Science\Computer Science Stuff\Python Projects\Mod.py", line 1, in <module>
    from Main import variable1, variable2
ImportError: cannot import name 'variable1' from 'Main' (D:\.here\Computer Science\Computer Science Stuff\Python Projects\Main.py)

I have no idea why this might happen.我不知道为什么会发生这种情况。 It contradicts what I've been doing in Java.这与我在 Java 中所做的相矛盾。

How would I be able to reference global variables that are stored in the main class if not through importing them?如果不通过导入它们,我如何能够引用存储在主 class 中的全局变量?

Your issue here is called a " circular dependency ".您在这里的问题称为“循环依赖”。 Main.py is trying to import a class from Mod.py, but before that can happen, Mod must import some variables from Main.py, but before that can happen... Main.py 正在尝试从 Mod.py 导入 class,但在此之前,Mod 必须从 Main.py 导入一些变量,但在此之前...

Generally, the way to solve a circular dependency is to reorganize how your program is laid out.通常,解决循环依赖的方法是重新组织程序的布局方式。 For instance, you might be able to parameterize Mod such that it no longer depends on Main.例如,您可以对 Mod 进行参数化,使其不再依赖于 Main。 For example:例如:

Main.py:主要.py:

from Mod import Mod

variable1=5
variable2=3

mod=Mod()

mod.task(variable1, variable2)

Mod.py:模组.py:

class Mod:
    def task(self, a, b):
        print(a + b)

Alternatively, you could store variable1 and variable2 in a different file.或者,您可以将variable1 1 和variable2 2 存储在不同的文件中。 Finding the best solution will depend on what makes the most sense for your program.找到最佳解决方案将取决于什么对您的程序最有意义。

You could update Mod.task() to receive two extra parameters, so you'd get:您可以更新 Mod.task() 以接收两个额外的参数,因此您将获得:

def task(self, a, b):
    print(a + b)

Then, in main, you would simply say:然后,主要是,你会简单地说:

mod.task(variable1, variable2)

No need for imports other than importing Mod into your main class除了将 Mod 导入您的主 class 之外,无需导入

This is caused by the Circular dependency这是由循环依赖引起的

As mentioned in the above answer, the way to solve a circular dependency is to reorganize how your program is laid out.正如上面的答案中提到的,解决循环依赖的方法是重新组织程序的布局方式。 Use parameters使用参数

Nevertheless, a simple workaround for this is by making inline imports尽管如此,一个简单的解决方法是进行内联导入

Main.py主文件

from Mod import Mod

variable1=5
variable2=3

mod=Mod()

mod.task()

Mod.py模组.py


class Mod:
    def task(self):
        from Main import variable1, variable2
        print(variable1+variable2)

Follow the DRY principles.遵循 DRY 原则。 Do Not Repeat Yourself.不要重复自己。

importing "Main" variables into Mod, is causing a circular dependency.将“主要”变量导入 Mod 会导致循环依赖。 Something you must avoid at all cost.您必须不惜一切代价避免的事情。 Use Object Oriente Programming to solve your problem.使用 Object Oriente Programming 来解决您的问题。

Steps to solve the problem:解决问题的步骤:

  1. Delete the import of Main.删除 Main 的导入。
  2. Change task function to allow two arguments to be passed.更改任务 function 以允许通过两个 arguments。 It should be something like this它应该是这样的

    def task(self, variable1, variable2): print(variable1 + variable2)

    #You can use any name for variables 1 and 2 #变量 1 和 2 可以使用任何名称

  3. On the Main script, just call the function:在主脚本上,只需调用 function:

     Mod.task(variable1, variable2)

    Here you must use the name of the variables, because you are now calling the definition you gave in Main.在这里您必须使用变量的名称,因为您现在正在调用您在 Main 中给出的定义。

I don't understand what You mean in Java such things can be done.. But it is kind of possible in python respecting the rule that the bodies of the circuar-referencing modules must be able to execute without access to the other involved modules content.我不明白你在 Java 中的意思是什么,可以做这样的事情。但是在 python 中,遵守循环引用模块的主体必须能够在不访问其他相关模块内容的情况下执行的规则是有可能的.
So You can do:所以你可以这样做:

# cat Main.py
import Mod
var1=2
var2=3
if __name__ == '__main__':
    mod=Mod.Mod()
    mod.task()
# cat Mod.py
import Main
class Mod(object):
    def task(self):
        print(Main.var1+Main.var2)

For example.例如。 Or handle the initialization of modules with functions You call after the import chain succeeded.或者使用导入链成功后调用的函数来处理模块的初始化。
Anyway most of the time it is better to avoid such stuff an just initializie You classes and call functions with parameters.无论如何,大多数时候最好避免这样的事情,而只是初始化你的类并使用参数调用函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM