简体   繁体   中英

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 ?

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

The correct way to call it is:

make_pizza(16, 'pepperoni')

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.

因为PATH环境变量中的模块目录不可用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM