Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Initially Structuring Your Project

In this this lesson, you’ll lay the groundwork for your project, including:

  • Reorganizing the file structure
  • Experimenting with different ways of running your script, with path or the -m flag
  • Exploring the __main__ module
  • Understanding a folder as a module

00:00 So, if we’re just looking at the console again,

00:03 so one kind of downside to this is that we’re calling a program called cli, and if you haven’t heard it before, it’s a kind of weird abbreviation for command-line interface, so it’s not really saying anything about the project we’re actually working with.

00:21 One thing that would be nice is to just have a better name for this. Of course, we could rename cli to be a better name, but it is kind of describing the role that this module plays in the project. So, an even better solution would be to actually create a directory that contains our project.

00:38 So, I guess we can do something like create the directory that we call maybe snakesay or something to that effect. That sounds good.

00:47 So I’ll just create a directory here, and we’ll call this snakesay. So, is this going to be the eventual name of the command? This will be the name of essentially our package, as we think of it.

00:59 And then we can see a few different ways that we can use this as a command, and it’ll definitely show up in some of the commands we’re using. Cool. So I will just move these files into that folder then, right? Yep. Perfect.

01:14 Okay. I’m going back to the command line now. If I was just to say python

01:22 snakesay, that’s probably not going to work, right? Let’s try it. Not quite, and here it’s actually pointing out the solution for us about something about the __main__ module. But before we do that, let’s try to now run python

01:37 snakesay\cli.py. Right. We’ll use just the path to the file. Exactly. And then we’ll have to put in some … let’s use the same …

01:50 message here. Yeah, cool. That works. It still works. Right, but that’s not ideal, because we’ve got sort of more to type now. We’re still having to call the CLI, which might not be obvious to someone who doesn’t know what a CLI is. Yes.

02:05 So, the way that Python has set up this is that you can actually call directories instead of Python files. To do this, Python then needs to kind of know, well, where is the entry point then inside of this directory?

02:18 Beause directory doesn’t make sense by itself. Yeah, because a directory isn’t a Python file. Right. It’s just a way to organize files. So what it kind of says within the big error message we’re getting here, it says, can't find '__main__' module inside of RealPython\\snakesay. Right, what does that mean? The __main__, which we often just call dunder main, is then just a convention really, but it’s being used actively by the Python interpreter to look for entry points, essentially—so, “What should I actually do when you call the directory?” And what it does then is that if there is a file that’s called __main__.py, then I call that file.

02:58 Okay. So we can start out just by renaming cli.py to be __main__.py. Okay, that sounds cool.

03:06 So if we take a CLI, and we’re calling __main__dunder being an abbreviation for double underscoreso that reads “underscore, underscore, main, underscore, underscore, dot py” is the new name of the file. Exactly. Okay, so what effect does this have?

03:26 Now we can go back and try to call snakesay one more time just directly. python snakesay. Okay, I haven’t passed any arguments in there, so let’s actually pass in some arguments.

03:44 Wow, cool. That’s a lot more readable. Yeah. So now, immediately we kind made our command to run this simpler, and we have, we kind of follow some of the conventions within Python.

03:56 So these __main__.py files, they might look a little bit weird and scary, but they’re a kind of convention that you can use everywhere, and you’ll get used to them and kind of know, “Okay, this is actually where my entry point to program is defined, so this is where the program will start running.” So you don’t call __main__.py usually explicitly.

04:17 You call the folder. It might not make much sense that you’re calling a folder, but the way Python works is that it says if someone were to call a folder—so they’d say python with the folder name—I’m going to treat that as a module, and if that module has a __main__, that means I can run that module.

Become a Member to join the conversation.