4

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?

Community
  • 1
  • 1
JoJo Hu
  • 41
  • 1
  • 2
  • 1
    "import pizza into make_pizza" makes no sense. `make_pizza` is a function defined *by* the module `pizza`. If `from pizza import make_pizza` fails, then you haven't configured your IDE properly to find the file `pizza.py`. – chepner Apr 24 '18 at 17:12
  • On reading this again, it's a little confusing, though, because it's not clear why you need a module `make_pizza` that calls `pizza.make_pizza`. – chepner Apr 24 '18 at 17:18

3 Answers3

4

You're importing it correctly, but you're calling it incorrectly.

The correct way to call it is:

make_pizza(16, 'pepperoni')
Weston Reed
  • 187
  • 2
  • 9
1

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.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 2
    Would just like to add, if you simply did `import pizza`, then your approach of using `pizza.make_pizza(16, "pepperoni")` would work. – SShah Apr 24 '18 at 21:34
0

because the module directory is not available in the PATH environment variable.

Sam TNT
  • 11
  • 2
  • I am pretty sure it is. – Xantium Apr 24 '18 at 19:50
  • You can import scripts from the same dictionary – Xantium Apr 24 '18 at 21:53
  • 1
    I put two .py files in the same folder "Project0",does it means the two .py files are in the same path? – JoJo Hu Apr 25 '18 at 06:44
  • @JoJoHu Yes there is no path problem in this case. The problem is how you imported the module. If you did put `Project0` in the path environment variable , you could import that project from anywhere. I.e. `import pizza` would work from any dictionary. – Xantium Apr 25 '18 at 08:04