How to run multiple python files

I have tried reading through previous ‘replit asks’ about this and I don’t understand I need to run another file based on user input as seen in the code snippet below. I scavenged the internet as well and all of them gave me false hope.

type = input("What Do You Want To Bake? (cookies,donuts,cake or Pretzeles)")

if (type == "cake"):
  exec(open(cake.py))

elif (type == "cookies"):
  exec(open(cookies.py))

elif (type == "donuts"):
  exec(open(donut.py))

elif (type == "Pretzeles"):
  exec(open(pretz.py))


for the code it is going to open another file that will bring them through the steps to bake that “sweet product” I am doing it this way for a cleaner look in my code. Thanks for all your help, have any questions please ask.
https://replit.com/@MasterLink/119-Capstone#main.py

You could just import the Py files.

# cake.py
def cake():
  # put code here
# main.py
type = input("Food? ")
if type == "cake":
  from cake import cake
  cake()
3 Likes

It worked at first but after the sub-file was completed its said the following

File "/home/runner/119-Capstone/main.py", line 4, in <module>
    from cake.py import cake
ModuleNotFoundError: No module named 'cake.py'; 'cake' is not a package
repl process died unexpectedly: exit status 1

I think when I ran it before the program started it installed a bunch of packages. One of those must have been when I imported all the python files is there a way to fix this?

Well now cake.py is not one function. It will only work if it’s all one function. You can use exec though.

if type == "cake":
  with open("cake.py", "r") as file:
    exec(file.read())

Note that a lot of people advise against using exec. I think it should be safe in this case as long as you leave no possible way for users to input anything to exec.

3 Likes

Don’t add .py when importing files.

2 Likes

I’d put the import in the top level of the module.

# main.py
from cake import cake

food = input("Food? ").lower()
if food == "cake":
  cake()

You will have to put your cake code in a function named cake in your cake.py file so that it doesn’t run immediately when you import it. Then, you call the cake function when you need to.

2 Likes

Ok some improvement but it will only play the first one listed.
…ss below…

any fixes?

import cake as cake
import donut as donut
import cookie as cookie
import pretz as pretz



type = input("What Do You Want To Bake? (cookies,donuts,cake or Pretzeles)").lower()

if type == "cake":
  with open("cake.py", "r") as file:
    exec(cake.read())

elif type == "donut":
  with open("donut.py", "r") as file:
    exec(donut.read())

elif type == "cookies":
  with open("cookie.py" "r") as file:
    exec(cookie.read())


elif type == "Pretzeles":
  with open("cookie.py", "r") as file:
    exec(pretz.read())


"""Help"""
if type == "cake":
  with open("cake.py", "r") as file:
    exec(cake.read())

You have imported cake on line 1 of your code. If you want to run it with exec, use exec(file.read() and not exec(cake.read()). Or you could use your import of the cake file. But right now you are combining two different methods and that’s why it isn’t working.

1 Like

No. We have provided you with code.

if type == "cake":
  with open("cake.py", "r") as file:
    exec(file.read())

You assigned the text in the file cake.py to a variable called file, thus you read it with file.read(). Use this code if you’re going to go the exec route.

Oh I apologize. I made a mistake in my previous code. The below code is not correct. cake.read() should be file.read().
image

1 Like

Thank you I think that worked : )
Sorry for the confusion from the previous post my dyslexia and disfunction to be able to read is to blame.

2 Likes

I’m glad I was able to help. I messed up in one of my sample codes too. :joy:

1 Like

That works I guess, but next time, I’d really recommend using an imported function instead of exec and file reading (can cause many problems).
exec is a function you should really never use in most programs.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.