Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Add a Main Module (Solution)

00:00 For this exercise, you should create a module called main.py that imports greet(). The first thing again is to save the file

00:12 and name it main.py. So that’s our main module here. Now is an important bit. Because you need to import greet() from greeter, that means you must save main.py next to greeter.

00:27 There would be ways to save it somewhere else, but in this case, it’s the most straightforward way to just save it next to greeter because that way you can import it without any big adjustments.

00:39 And I will show you how. Again, the filename is main.py and you save it next to greeter.

00:48 I will add the filename on top, and now just like last time, I will add comments of the things that you need to do. So create a module named main.py. We did this, and now it should import greet() from greeter.py.

01:09 Now the interesting thing is if you’re just listening to this, that Python is very close to the English language. So we almost have the solution in this comment already.

01:18 But let’s continue with the comments for now. The next part is call the function with the argument "Real Python" as a string.

01:34 Let’s start with import greet() from greeter.py.

01:39 There are multiple ways how you can import modules and their names. And if you need a refresher, you can visit the Python Basics course Modules and Packages.

01:50 For this exercise, it’s a good idea to import the module as a whole. So the import statement is import greeter. Let’s save the file and run it to see if there are any errors.

02:05 There are no errors, so that means main.py is okay so far. We have imported greeter, and that means we also have imported the greet() function.

02:15 To access it, we need to write the module greeter,

02:20 the function name.

02:23 Remember that greet() takes one argument, which is name. So if we run this right now, we’ll get an error. And for this moment, it’s okay to get an error because that way we also see that we are actually accessing the greet() function.

02:37 So let’s save it and run it. Just like expected, we get a TypeError: greet() is missing 1 required positional argument, which is name. That is the last part of this exercise: call the function with the argument "Real Python".

02:55 That means we have solved the import greet() from greeter.

03:00 Now maybe at this point you spot something interesting because I didn’t solve the exercise exactly like it was requested. The request was to import greet() from the greeter module, but I actually imported the greeter module and not the greet() function.

03:16 In the end, the solution still works, but you might have come up with a different one, and maybe yours is even more correct to the exercise request than mine.

03:27 And now you need to call the function with the argument "Real Python". So let’s copy that "Real Python" string from the comment and paste it as an argument in greeter.greet() inside the parentheses, save it, and run it.

03:45 And as you can see on the left side in the interactive panel, it says Hello, Real Python! Perfect. So that means the greet() function is called with the "Real Python" string.

03:54 You solved the exercise. You can remove the comment and save the file.

Become a Member to join the conversation.