-3

I'm brand new to Python, or programming in general and I'm working through the book "Python Crash Course". Right now I am learning about modules and the first thing I'm learning is about the import function.

I have two files. making_pizza.py and pizza.py

In pizza.py, I have the following

def make_pizza(size, *toppings):
"""Summarize the pizza we are about to make"""
print("\nMaking a " + str(size) +
      "-inch pizza with the following toppings:")
for topping in toppings:
    print("- " + topping)

Then in making_pizza.py I have the following.

import pizza

make_pizza(16, 'sausage')

I get the error "NameError: name 'make_pizza' is not defined" and import pizza is greyed out.

However, if I use

from pizza import make_pizza:

I can use the function.

Can someone explain why I am unable to use the "Import" function in Python? I'm using PyCharm as my interpreter.

  • if you use just `import pizza` then you will have to do `pizza.make_pizza` since you have to specify where `make_pizza` comes from. Doing `from pizza import make_pizza` already makes clear where it comes from so you can use it directly – Sembei Norimaki May 20 '22 at 11:57
  • `import pizza` makes the name `pizza` (the entire module) available in your file. It does *not* implicitly import all names from the `pizza` module. Python is fairly explicit about that. – deceze May 20 '22 at 11:57
  • If you do `import pizza` you have to call the function with `pizza.make_pizza(16, 'sausage')`. – Matthias May 20 '22 at 11:57

0 Answers0