0

I developed a python 3.6.6 console app that does some actions. Here is the structure I'm using in this app.py file:

#!/usr/bin/python3

import threading
import time

# My app content
if __name__ == '__main__':
    # all may code is in here

Now I need to call this same app in a parent app that does other actions. What do I put in the parent_app.py file:

#!/usr/bin/python3

#some imports...

#the call to the app.py, but how since app.py hasn't got functions in it???

Thanks for your help to this basic question... I'm new in Python as you can see ;-)

Ephie
  • 229
  • 2
  • 13
  • 1
    Ironically, whether you realised it or not, you put your code in *the* one place where the code is *explicitly prevented* from running when imported as a module because of the condition. – Paritosh Singh Nov 20 '19 at 11:38
  • 5
    Does this answer your question? [In Python, can I call the main() of an imported module?](https://stackoverflow.com/questions/14500183/in-python-can-i-call-the-main-of-an-imported-module) – Kha Nguyễn Nov 20 '19 at 11:44

6 Answers6

6

Having all your content inside the if statement would be a bad idea anyway, at the very least, put everything you have inside that into a function and have that function called from inside the if statement.

# My app content
def func():
    # all may code is now in here   

if __name__ == '__main__':
    func()

Now you have a function you can import! But really you should start looking at splitting your code into multiple functions and code structure in general

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • you should probably add how to use the function in the module (`import module_name` `module_name.func()` etc) – Derte Trdelnik Nov 20 '19 at 11:28
  • @DerteTrdelnik - I was going to but the op's second snippet seems to make it clear to me that they already know how to import a function. – Sayse Nov 20 '19 at 11:29
1

Define a function called main or whatever u prefer in app.py

def main():
  ## TODO

if __name__ == '__main__':
  ## Encapsulate all your code inside this main function
  main()

In parent_app.py, you can import app.py and call the function main as follow:

import app
app.main()
Kha Nguyễn
  • 463
  • 1
  • 4
  • 16
1

parent_app.py

#-*- coding:utf-8 -*-

from script_to_load import app_function,call_method

#if you just wanted app_functions
app_function()
#if you wanted to be script get executed as it is going to execute on app.py run 
call_method()

app.py

#-*- coding:utf-8 -*-

def app_method():
    #do somthing

def call_method():
    app_method()
    print("not with if __name__ == '__main__ '")
if __name__ == "__main__":
    app_method()   
    print("with if __name__ == '__main__'")

as we know Every module in Python has a special attribute called name. The value of name attribute is set to 'main' when module run as main program. Otherwise, the value of name is set to contain the name of the module. I hope it will works for you

  • Thanks for the explanation on how **if __name__ == "__main__":** works. I was wondering about the reason of this code... So I guess you can have different behaviors if the app is called as **script** or as a **module** of an other app.. – Ephie Nov 20 '19 at 12:24
0

just use the import functionality and the name of the file

from app import *

Here the * signifies that all contents from the file is imported.

If you want to import only one function or class from the python file, consider doing

from app import function_name

or

from app import class_name
Shagun Sodhani
  • 3,535
  • 4
  • 30
  • 41
0

Perhaps you may also want to look into creating a class!

app.py:

class Example(parameters): //insert code here

parent.py:

from app import Example //use (instantiate) the class

Gabriel
  • 438
  • 1
  • 5
  • 16
0

There are three ways of doing this -

  1. You can import the file in the parent file.

    import app

  2. Using the exec function.

    exec('app.py')

  3. Making use of os.system(filename)

    import os

    os.system('app.py')