RedSox2
RedSox2

Reputation: 17

How do you execute code from another file in an if statement?

I have a project, that I am working on with only one file (main.py) and the code just gets really messy with lots and lots of if statements and things, so I can make new files, but how do I get the main.py file to execute the code in another file? Here is an example:

if condition:
  execute code in another file

Upvotes: 0

Views: 1317

Answers (2)

ZERO
ZERO

Reputation: 93

if you want to use funtions inside other files you can use

from otherFile import someFunction

or

from otherFile import *

otherFile has to be replaced with the name of the python file without the .py

Edit: import * imports every function inside the file

Upvotes: 0

John Gordon
John Gordon

Reputation: 33285

If the code in the other file is organized into a function, you can import it and call it:

from othermodule import otherfunction

if condition:
    otherfunction()

Upvotes: 1

Related Questions