-1

i want to run another file into one script and run continously with loop... in the first loop all import file works fine...

  • def my_function_1
  • def my_function_2
  • def my_function_3 #import
  • def my_function_4 #import
  • def my_function_5

but in the seconds loop, those all import file not working and the work flow jump, with flow like this:

  • def my_function_1
  • def my_function_2
  • def my_function_5

this the code ive try:

acc = 'infos/acc-list.json'
f = open(acc,)
datas = json.load(f)

def main(data):

    def my_function_1():
        print('Login')
    my_function_1()

    def my_function_2():
        print('Create new Post')
    my_function_2()

    def my_function_3():
        import anotherFile1 #python File to do Liking
    my_function_3()

    def my_function_4():
        import anotherFile2 #python File to do Comments
    my_function_4()

    def my_function_5():
        print('Log Out')
    my_function_5()

for data in datas:
    main(data)

So. after logout, it will run again with another account from list in acc-list.json

and in the second loop / second account, those function_3 and function_4 not execute....

so the flow just like:

  • login # function_1
  • Create new post # function_2
  • log Out # Function_5
  • 3
    What loop? what import? what does *not working* mean? please provide [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Guy Jul 05 '22 at 05:44
  • oke i will update my question – Bobby Wibowo Jul 05 '22 at 05:48
  • if you import something inside function, it wont import to the whole system. It is limited to function only. – Prakash Dahal Jul 05 '22 at 05:49
  • @PrakashDahal in the first run, all function are executed, but after run at the second, the import function didnt executed.. so how the solution? – Bobby Wibowo Jul 05 '22 at 06:03
  • 2
    In the second run, the `import` *statement* (not a function) *does* execute. However, there is a cache of results (the `module` objects that are produced by importing), and the cached value is given back immediately. Please see the linked duplicates. – Karl Knechtel Jul 05 '22 at 06:09
  • @KarlKnechtel oke i will go to link – Bobby Wibowo Jul 05 '22 at 06:44

1 Answers1

0

By importing a module (that only contains functions) you do not execute anything. If you wish to run a function from within a separate module you should write:

def my_function_3():
    import anotherFile1
    anotherFile1.func()
my_function_3()

or

def my_function_3():
    from anotherFile1 import funct
    func()
my_function_3()

where func is the name of the function inside the module anotherFile1.

  • 1
    That's not entirely true, as importing modules can lead to execution of code – EDG956 Jul 05 '22 at 06:07
  • *In general*, importing modules executes code. A `def` statement *is code that is executed* when it is encountered - which results in creating the function object and assigning it to a name. Similarly for `class`. – Karl Knechtel Jul 05 '22 at 06:12
  • @EDG956, of course you are correct, but in my view (which is based on coding style) each the module will not contain anything that explicitly executes by importing because the module you contain only functions and classes where all the execution is enclosed under `if __name__ == '__main__'`. But this is not true in the example above, as you have correctly stated. I will edit my answer to take account of this. – Eliyahu Shwartz Jul 05 '22 at 06:17