Importing functions between two Python scripts simultaneously










0














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.



The issue...



I am able to have both scripts access each other using import main and then main.functionName() to call the function. 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



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


For example the following code;



TESTING.py



import TESTING2

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

print("begin")
TESTING2.startOver()


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:UsersharryDesktopTESTING.py", line 1, in <module>
import TESTING2
File "C:UsersharryDesktopTESTING2.py", line 1, in <module>
import TESTING
File "C:UsersharryDesktopTESTING.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.










share|improve this question





















  • The way you've constructed this doesn't make much sense. You should separate functionality you want to import from where it is being run. Just create a module my_module and place both runme and startOver there. Then in TESTING2 do: from my_module import runme, startOver
    – Karl
    Nov 10 '18 at 8:22











  • The point of this program is to be able to essentially edit itself. I want to separate the commands from the main script so that more commands can be simply added on the bottom of commands.py while main.py is running and then be used by main.py (unless it is possible to edit and update a python script in real time while running). The example above is simply that - an example. It's a simple code that causes the same error I get when running the actual program :)
    – Harry Cossie
    Nov 10 '18 at 9:37










  • Ok, so let's say while main.py is running I add a function my_new_fuction to commands.py. Now what? Do you want to now use this in main.py? If so, how? You use the "input" function above, so is that how you plan to tell main to execute my_new_func?
    – Karl
    Nov 10 '18 at 13:27















0














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.



The issue...



I am able to have both scripts access each other using import main and then main.functionName() to call the function. 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



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


For example the following code;



TESTING.py



import TESTING2

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

print("begin")
TESTING2.startOver()


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:UsersharryDesktopTESTING.py", line 1, in <module>
import TESTING2
File "C:UsersharryDesktopTESTING2.py", line 1, in <module>
import TESTING
File "C:UsersharryDesktopTESTING.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.










share|improve this question





















  • The way you've constructed this doesn't make much sense. You should separate functionality you want to import from where it is being run. Just create a module my_module and place both runme and startOver there. Then in TESTING2 do: from my_module import runme, startOver
    – Karl
    Nov 10 '18 at 8:22











  • The point of this program is to be able to essentially edit itself. I want to separate the commands from the main script so that more commands can be simply added on the bottom of commands.py while main.py is running and then be used by main.py (unless it is possible to edit and update a python script in real time while running). The example above is simply that - an example. It's a simple code that causes the same error I get when running the actual program :)
    – Harry Cossie
    Nov 10 '18 at 9:37










  • Ok, so let's say while main.py is running I add a function my_new_fuction to commands.py. Now what? Do you want to now use this in main.py? If so, how? You use the "input" function above, so is that how you plan to tell main to execute my_new_func?
    – Karl
    Nov 10 '18 at 13:27













0












0








0







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.



The issue...



I am able to have both scripts access each other using import main and then main.functionName() to call the function. 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



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


For example the following code;



TESTING.py



import TESTING2

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

print("begin")
TESTING2.startOver()


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:UsersharryDesktopTESTING.py", line 1, in <module>
import TESTING2
File "C:UsersharryDesktopTESTING2.py", line 1, in <module>
import TESTING
File "C:UsersharryDesktopTESTING.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.










share|improve this question













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.



The issue...



I am able to have both scripts access each other using import main and then main.functionName() to call the function. 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



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


For example the following code;



TESTING.py



import TESTING2

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

print("begin")
TESTING2.startOver()


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:UsersharryDesktopTESTING.py", line 1, in <module>
import TESTING2
File "C:UsersharryDesktopTESTING2.py", line 1, in <module>
import TESTING
File "C:UsersharryDesktopTESTING.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.







python python-import






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 '18 at 7:32









Harry Cossie

1




1











  • The way you've constructed this doesn't make much sense. You should separate functionality you want to import from where it is being run. Just create a module my_module and place both runme and startOver there. Then in TESTING2 do: from my_module import runme, startOver
    – Karl
    Nov 10 '18 at 8:22











  • The point of this program is to be able to essentially edit itself. I want to separate the commands from the main script so that more commands can be simply added on the bottom of commands.py while main.py is running and then be used by main.py (unless it is possible to edit and update a python script in real time while running). The example above is simply that - an example. It's a simple code that causes the same error I get when running the actual program :)
    – Harry Cossie
    Nov 10 '18 at 9:37










  • Ok, so let's say while main.py is running I add a function my_new_fuction to commands.py. Now what? Do you want to now use this in main.py? If so, how? You use the "input" function above, so is that how you plan to tell main to execute my_new_func?
    – Karl
    Nov 10 '18 at 13:27
















  • The way you've constructed this doesn't make much sense. You should separate functionality you want to import from where it is being run. Just create a module my_module and place both runme and startOver there. Then in TESTING2 do: from my_module import runme, startOver
    – Karl
    Nov 10 '18 at 8:22











  • The point of this program is to be able to essentially edit itself. I want to separate the commands from the main script so that more commands can be simply added on the bottom of commands.py while main.py is running and then be used by main.py (unless it is possible to edit and update a python script in real time while running). The example above is simply that - an example. It's a simple code that causes the same error I get when running the actual program :)
    – Harry Cossie
    Nov 10 '18 at 9:37










  • Ok, so let's say while main.py is running I add a function my_new_fuction to commands.py. Now what? Do you want to now use this in main.py? If so, how? You use the "input" function above, so is that how you plan to tell main to execute my_new_func?
    – Karl
    Nov 10 '18 at 13:27















The way you've constructed this doesn't make much sense. You should separate functionality you want to import from where it is being run. Just create a module my_module and place both runme and startOver there. Then in TESTING2 do: from my_module import runme, startOver
– Karl
Nov 10 '18 at 8:22





The way you've constructed this doesn't make much sense. You should separate functionality you want to import from where it is being run. Just create a module my_module and place both runme and startOver there. Then in TESTING2 do: from my_module import runme, startOver
– Karl
Nov 10 '18 at 8:22













The point of this program is to be able to essentially edit itself. I want to separate the commands from the main script so that more commands can be simply added on the bottom of commands.py while main.py is running and then be used by main.py (unless it is possible to edit and update a python script in real time while running). The example above is simply that - an example. It's a simple code that causes the same error I get when running the actual program :)
– Harry Cossie
Nov 10 '18 at 9:37




The point of this program is to be able to essentially edit itself. I want to separate the commands from the main script so that more commands can be simply added on the bottom of commands.py while main.py is running and then be used by main.py (unless it is possible to edit and update a python script in real time while running). The example above is simply that - an example. It's a simple code that causes the same error I get when running the actual program :)
– Harry Cossie
Nov 10 '18 at 9:37












Ok, so let's say while main.py is running I add a function my_new_fuction to commands.py. Now what? Do you want to now use this in main.py? If so, how? You use the "input" function above, so is that how you plan to tell main to execute my_new_func?
– Karl
Nov 10 '18 at 13:27




Ok, so let's say while main.py is running I add a function my_new_fuction to commands.py. Now what? Do you want to now use this in main.py? If so, how? You use the "input" function above, so is that how you plan to tell main to execute my_new_func?
– Karl
Nov 10 '18 at 13:27












1 Answer
1






active

oldest

votes


















0














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. 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. 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.



As a small example, try setting up commands.py and main.py scripts as follows:



#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:



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

def func2():
print(2)


Now hit "enter" in the running main.py script. You should see the result of func2printed to the terminal.



Note however also that reload doesn't necessarily act the way you would expect and could cause some strange and explainable things to happen. For more info, see this post: https://stackoverflow.com/a/438845/141789






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236928%2fimporting-functions-between-two-python-scripts-simultaneously%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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. 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. 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.



    As a small example, try setting up commands.py and main.py scripts as follows:



    #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:



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

    def func2():
    print(2)


    Now hit "enter" in the running main.py script. You should see the result of func2printed to the terminal.



    Note however also that reload doesn't necessarily act the way you would expect and could cause some strange and explainable things to happen. For more info, see this post: https://stackoverflow.com/a/438845/141789






    share|improve this answer

























      0














      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. 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. 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.



      As a small example, try setting up commands.py and main.py scripts as follows:



      #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:



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

      def func2():
      print(2)


      Now hit "enter" in the running main.py script. You should see the result of func2printed to the terminal.



      Note however also that reload doesn't necessarily act the way you would expect and could cause some strange and explainable things to happen. For more info, see this post: https://stackoverflow.com/a/438845/141789






      share|improve this answer























        0












        0








        0






        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. 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. 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.



        As a small example, try setting up commands.py and main.py scripts as follows:



        #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:



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

        def func2():
        print(2)


        Now hit "enter" in the running main.py script. You should see the result of func2printed to the terminal.



        Note however also that reload doesn't necessarily act the way you would expect and could cause some strange and explainable things to happen. For more info, see this post: https://stackoverflow.com/a/438845/141789






        share|improve this answer












        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. 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. 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.



        As a small example, try setting up commands.py and main.py scripts as follows:



        #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:



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

        def func2():
        print(2)


        Now hit "enter" in the running main.py script. You should see the result of func2printed to the terminal.



        Note however also that reload doesn't necessarily act the way you would expect and could cause some strange and explainable things to happen. For more info, see this post: https://stackoverflow.com/a/438845/141789







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 '18 at 7:42









        Karl

        2,29443055




        2,29443055



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236928%2fimporting-functions-between-two-python-scripts-simultaneously%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)