Search code examples
python-3.xerror-handlingattributeerror

Importing a method from user-defined module gives Attribute Error


I am going through python crash course book and i want to import a module that i have written its function and saved it properly in the path. using pycharm, python 3.7

I have properly written the code and i know it exists(the attribute) in the imported module.

the code is this:

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

but i get this error AttributeError: module 'pizza' has no attribute 'make_pizza'

I dont know what to do,i really need to solve this problem to be able to continue my learning.

Here is what i do to import it .


Solution

  • You should save your module and then check for the directory structure, if it is like,

    |-pizza/
    |--pizza.py
    

    Then try this for importing pizza module in another python program:

    from pizza import pizza
    pizza.make_pizza(10,...)
    

    I hope this helps. This maybe because of your package structure.