简体   繁体   English

同时在两个Python脚本之间导入函数

[英]Importing functions between two Python scripts simultaneously

I am trying to run a chat bot of sorts that is capable of creating new commands whilst the program is running. 我正在尝试运行一种聊天机器人,该机器人能够在程序运行时创建新命令。 To do this I am keeping all the commands in a second python script and using the main script to edit the commands.py file whilst the chat bot is still running. 为此,我将所有命令保留在第二个python脚本中,并在聊天机器人仍在运行时使用主脚本来编辑commands.py文件。

The issue... 问题...

I am able to have both scripts access each other using import main and then main.functionName() to call the function. 我可以使两个脚本使用import main相互访问,然后使用main.functionName()调用该函数。 However, when I try to call a function in commands.py from main.py, then use the function called to call another function back in main.py I get an error saying 但是,当我尝试从main.py调用commands.py中的一个函数,然后使用被调用的函数在main.py中调用另一个函数时,我收到一条错误消息:

AttributeError: module 'main' has no attribute 'exampleFunction' 

For example the following code; 例如下面的代码;

TESTING.py TESTING.py

import TESTING2

def runme(inp):
    print(inp)
    startOver()

print("begin")
TESTING2.startOver()

TESTING2.py TESTING2.py

import TESTING

def startOver():
    userInput = input("Enter text at your own risk... ")
    TESTING.runme(userInput)

Produces the following; 产生以下内容;

begin
Traceback (most recent call last):
  File "C:\Users\harry\Desktop\TESTING.py", line 1, in <module>
    import TESTING2
  File "C:\Users\harry\Desktop\TESTING2.py", line 1, in <module>
    import TESTING
  File "C:\Users\harry\Desktop\TESTING.py", line 8, in <module>
    TESTING2.startOver()
AttributeError: module 'TESTING2' has no attribute 'startOver'

The desired outcome would be a continuous loop of entering an input and then the text being printed as if one seamless script. 期望的结果将是一个连续的循环:输入一个输入,然后像打印一个无缝脚本一样打印文本。

Is this possible? 这可能吗? If so how do I do it - or is there a better way to achieve the same goal? 如果是这样,我该怎么做-还是有更好的方法实现相同的目标?

Many thanks. 非常感谢。

So, I'll have a go at giving you something that might solve your problem. 因此,我将尽一切努力为您解决问题。 Essentially what you are doing is constructing a circular dependency: commands.py is written by main.py , main.py depends on commands.py for its functions. 本质上,您正在做的是构造循环依赖项: commands.pymain.py编写, main.py的功能依赖于commands.py There is almost certainly a way to solve your problem without introducing such a circular dependency, but I would need to know more in order to suggest something. 几乎可以肯定有一种无需引入循环依赖就可以解决您的问题的方法,但是我需要了解更多以便提出一些建议。

If you are sure you want to do it like this, you could use importlib.reload , which tells python to reload a module that you've already imported. 如果确定要这样做,可以使用importlib.reload ,它告诉python重新加载已经导入的模块。 In other words, if you've added a new function to commands.py since calling the original import , calling reload will now make this function available. 换句话说,如果自从调用原始import以来,已在commands.py添加了新功能,则调用reload现在将使该功能可用。

As a small example, try setting up commands.py and main.py scripts as follows: 作为一个小示例,请尝试按如下所示设置commands.pymain.py脚本:

#commands.py
def func1():
    print(1)

and: 和:

#main.py
import commands
commands.func1()

input("hit enter once you've edited commands.py")

from importlib import reload
commands = reload(commands)
commands.func2()

run main.py and when you get to the input part, open up commands.py and change it to look like this: 运行main.py ,当您进入input部分时,打开commands.py并将其更改为如下所示:

#commands.py
def func1():
    print(1)

def func2():
    print(2)

Now hit "enter" in the running main.py script. 现在,在正在运行的main.py脚本中单击“输入”。 You should see the result of func2 printed to the terminal. 您应该看到func2的结果打印到终端上。

Note however also that reload doesn't necessarily act the way you would expect and could cause some strange and explainable things to happen. 但是请注意, reload并不一定像您期望的那样运行,并且可能导致某些奇怪且可解释的事情发生。 For more info, see this post: https://stackoverflow.com/a/438845/141789 有关更多信息,请参阅此帖子: https : //stackoverflow.com/a/438845/141789

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

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