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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

Adding a Command Script

In this lesson, you’ll add some nifty configurations, namely:

  • A project.scripts section in pyproject.toml
  • A callable function in __main__.py

You’ll also learn about when you might need to reinstall your project.

00:00 What if we wanted to take this the last step further and just say, “Is it possible for me, for example, if I’m just here just to say snakesay and have something run?” Yes, it is, and what we’re talking about then is essentially having an entry point script or converting our Python script into an entry point or something like this, where it’s recognized by the terminal that you’re running in, and that is supported by the pyproject.toml.

00:31 So we could jump back a little bit to the documentation for this. So, in the documentation, you can find at the bottom there, there’s something called project.scripts.

00:40 And this gives us the possibility to add in these kind of scripts that can just be run as if they’re terminal commands. So we can add a project.scripts section to our pyproject.toml file.

00:57 And here, this example is kind of nice because it kind of shows the different parts that we need to specify. So the my-script part there is just the name of your script.

01:06 So this one, we can— Say snakesaysnakesay, for instance, although just to be clear, it doesn’t need to be the name of the folder or anything like this. It’s an independent—We can just call it snake or whatever we want. snakey. snakey.

01:20 And then the package—so now we’re into the kind of Python-land, so we have the package, module, and function. Okay. So here we need now to think about, “Okay, what is the package that we want to call?” And that is the snakesay package.

01:36 What is the module that we want to call? And that is the __main__ thing, so here we’ll just be __main__. Okay, we have to be explicit with the __main__ here. Yes.

01:48 And then there’s a function, and now we should go back to our __main__.py file and see which function do we want to call. And you can see here that there is no function for us, so that gives us one small little change that we can also make here, is to create a function that we can call.

02:05 So if we put this snake.say() into a main() function, for instance,

02:11 and then, yeah, just move this one in there. And now if we want to just keep this still functioning as it used to, we should call

02:20 main() at the bottom so that if you call __main__.py, it runs the main() function, but it also gives us a reference that we can use in pyproject.toml. Right. Okay, so we need this little change to make this script work. Yeah. And we can just call main here. Exactly.

02:37 Great. So now you can—let’s just do this. If we go to the command line and type snakey, you’ll see that that doesn’t work. Right, even though it’s an editable install. Right.

02:49 This is something that you need to do something else to actually get it working. Yeah. So the editable install will pick up anything you change in the code itself, so it has picked up that we now have a main() function. Right. But it hasn’t picked up that we have defined something new in the pyproject.toml file. Okay.

03:06 So when we do changes in the pyproject.toml file, we need to pip install it again. Okay. And you just do the same command from the same place. Exactly.

03:19 From here, python

03:23 -m pip install -e . Yep.

03:35 Okay. Oh yes, I see that it found the existing, uninstalled it, and then installed the new version. Yep, Exactly. Okay, great. So that means that now I can just go wherever I want and call snakey. Yes, you can.

Become a Member to join the conversation.