问题
Let's say I have a file like:
Project0\pizza.py
Project0\make_pizza.py
and pizza:
def make_pizza(size,*toppings):
print("\nMaking a " + str(size)
+ "-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
and make_pizza:
from pizza import make_pizza
pizza.make_pizza(16, 'pepperoni')
and as shown in the codes, I want to import pizza
into make_pizza
, but the IDE shows up an error that there is no module named pizza. How can I solve this problem and import pizza
into make_pizza
?
回答1:
You're importing it correctly, but you're calling it incorrectly.
The correct way to call it is:
make_pizza(16, 'pepperoni')
回答2:
You imported only the function make_pizza
in your make_pizza.py
so you can just use make_pizza
without redefining pizza
(since Python has already loaded this):
from pizza import make_pizza
make_pizza(16, 'pepperoni')
As mentioned in the comments below you could use this function, but then you would need to import pizza
and not just part of it.
回答3:
because the module directory is not available in the PATH environment variable.
来源:https://stackoverflow.com/questions/50007372/how-to-import-my-own-modules-in-python-3-6