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

Make a Runnable Script

00:00 Make a Runnable Script. In this section, you’ll provide a command-line interface to your Python package By using argparse. It will take a path to a binary file with your maze as input.

00:13 Then it will try to find the maze’s solutions and render them in separate tabs in your web browser as scalable vector graphics. If it can’t find any solutions, then you’ll see an appropriate message in your console.

00:27 Add the __main__.py file to your project to turn it into a runnable Python package. This file should live inside your maze_solver package next to the empty __init__.py file.

00:41 Its contents are seen on-screen.

01:21 The code finds, renders, and previews all the solutions of the loaded maze or prints an error message.

01:36 It uses the argparse module to parse the file path, which is currently the only expected command-line argument. Later, you can add more if you want to—for example, to allow previewing mazes without a solution. Finally, the file uses the name-main idiom to protect the main() function from running if other modules import the package.

02:06 If you want to know more about using argparse, then check out this Real Python course. To run the package, use the command seen on-screen in your terminal.

02:30 Optionally, you can specify a shortcut in your pyproject.toml file. You can have as many custom commands as you like, which should map to specific functions in the given module within the project.

02:51 To register the new command in your virtual environment, you must install the package using pip once more.

03:03 Once you’ve reinstalled the package, you’ll be able to solve mazes using the solve command without explicitly calling on Python.

03:16 Note that you need to run it from your virtual environment. The solve command, which you just created, is not available outside of it. You now have a working command-line interface to your Python package and a working maze solver that you can access quickly from the command line or terminal. While you’ve reached the end of this two-part course, remember that you can always keep improving the project. For example, you could add more command-line arguments to control the rendering process or provide an option to save the results as an image instead of previewing them in the browser. The possibilities are endless, but in the next part of the course, you’ll look back at what you’ve learned.

Become a Member to join the conversation.