JoJo Hu
JoJo Hu

Reputation: 41

How to import my own modules in python 3.6?

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?

Upvotes: 4

Views: 6731

Answers (3)

Xantium
Xantium

Reputation: 11605

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.

Upvotes: 1

Weston Reed
Weston Reed

Reputation: 187

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

The correct way to call it is:

make_pizza(16, 'pepperoni')

Upvotes: 4

Sam TNT
Sam TNT

Reputation: 11

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

Upvotes: 0

Related Questions