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