0

I have two python files.

I need to call the 2nd one from 1st.

I'm aware of os.system() commands.

But it's been mentioned as the worst way to execute a python script.

The script I need to call doesn't contain any functions or classes. but just the flow.

what is the better way to execute the script from another script.

Sukumar
  • 171
  • 10
  • Importing a python file is not the same as "executing" a python file which may be its own program with command line arguments, side-effects, different Python version, etc. This is not the same question. – John Hanley Aug 26 '18 at 19:16

2 Answers2

0
  1. Put both files in the same directory.

  2. Add an empty file called __init__.py in the directory.

  3. Then in the first file just include this: from file2 import myfunction

  4. You can then call myfunction from file2 in file1.

Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
0

by importing you execute everything in your other .py file that is outside a function.

if im inside template.py but want to run code inside test.py that for example only opens a gui window, all i need to do is type import test and have the map structure like this enter image description here

Stanley
  • 2,434
  • 18
  • 28