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 ;-)

2
  • 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. Commented Nov 20, 2019 at 11:38
  • 5
    Does this answer your question? In Python, can I call the main() of an imported module? Commented Nov 20, 2019 at 11:44

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

2 Comments

you should probably add how to use the function in the module (import module_name module_name.func() etc)
@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.
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()

Comments

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

1 Comment

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

Comments

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

Comments

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.