How to import my own modules in python 3.6?

£可爱£侵袭症+ 提交于 2020-06-27 10:39:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!