How to import a class from another module

Hi all,

For my python course, I’d like to import the Restaurant class from the module restaurant.py to module three_restaurants.py.

In three_restaurants.py, I want to initialize three instances from the class Restaurant.

How do I import the class? Can I import the class Restaurant?

Now pycharm raises the error

Unused import statement ‘from restaurant import Restaurant’

Here is the code from restaurant.py

# Class restaurant

class Restaurant:
    """"This class describes a restaurant"""

    def __init__(self, restaurant_name, cuisine_type):
        """"Initialize name and age attributes"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        """"Method to describe the restaurant"""
        print("Welcome to " + self.restaurant_name.title() + ", enjoy its " + self.cuisine_type + " cuisine.")

    def open_restaurant(self):
        """"Message to inform the customers that the restaurant is open"""
        print(self.restaurant_name.title() + " is open. Please come in and enjoy your dinner.")


# creating an instance of the restaurant
restaurant = Restaurant('yaff', 'haute')

restaurant.describe_restaurant()
restaurant.open_restaurant()

Thanks in advance

In restaurant.py:

class Restaurant:
    ...

In your other file three_restaurants.py:

from restaurants import Restaurant

joes_diner = Restaurant()
moms_kitchen = Restaurant()
golden_lotus_chinese_pizza = Restaurant()

The Pycharm warning (not an error!)

Unused import statement ‘from restaurant import Restaurant’

will go away once you actually use that class Restaurant…

Wow, so simple, thanks Steven for the explanation and the example.

Also, good to know that it’s a warning instead of error.

I’m also having troubles importing a method from a class out of another module.

I have this module users.py, and I want to import one of the methods show_privileges from the Admin class into module privileges.py (there are two classes in users.py)

Unfortunately, pycharm states: ‘Cannot find reference show_privileges in users.py’

How can I resolve this?

For some reason I can only add one picture in my comment.

Here’s the code that contains the show_privileges method.

class Admin(User):
    """
    This class is based on the class User
    """

    def __init__(self, first_name, last_name, age, gender):
        super().__init__(first_name, last_name, age, gender)
        self.privileges = ["it can add a post", "it can delete a post", "it can ban a user"]

    def show_privileges(self):
        print("An Admin has the following privileges:")
        for privilege in self.privileges:
            print("- " + privilege.capitalize())


admin = Admin('jeffrey', 'jongkees', 37, 'm')
admin.show_privileges()

Jeff,

You would have to initialise the Admin class to use your function as it isn’t agnostic and can’t be imported separately.

Could you explain what you mean by “agnostic” here? This is not a use of the word with which I am familiar.

Do you mean that show_privileges is not a top level name, but rather a method inside Admin, which is a top level name.

Thanks,
Cameron Simpson

Yes, exactly. That’s what I meant. Thank you for simplifying it.

Hi Abdullah,

Thank you very much for explaining. I’m only a beginner, and couldn’t figure it out. Have a great day

No problem at all. You can DM me anytime if you need help.

Great. I will definitely do.