Loading video player…
Structuring Your Python Script (Overview)

Structuring Your Python Script (Overview)

You may have begun your Python journey interactively, exploring ideas within Jupyter Notebooks or through the Python REPL. While that’s great for quick experimentation and immediate feedback, you’ll likely find yourself saving code into .py files. However, as your codebase grows, knowing where things should go in your script becomes increasingly important.

Transitioning from interactive environments to structured scripts helps promote readability, enabling better collaboration and more robust development practices. This video course shows you the foundations of organizing a Python script: where the runnable bits go, how to arrange your imports, and how to refactor with constants and a fixed entry point.

By the end of this video course, you’ll know how to:

  • Make a script directly executable on Unix-like systems with a shebang line
  • Organize your import statements using standard grouping conventions
  • Automatically sort imports and format code with the ruff linter
  • Replace hard-coded values with meaningful constants
  • Define a clear script entry point using if __name__ == "__main__"

Without further ado, it’s time to start working through a concrete script and progressively shape it into well-organized, shareable code.

Download

Course Slides (.pdf)

2.2 MB
Download

Sample Code (.zip)

1.2 KB

00:00 Welcome to the Real Python course “Structuring Your Python Script”. My name is David, and I’ll be your instructor on this video course. Have you been learning Python for a while and you want to move beyond the interactive learning environment that is the Python REPL and start writing some Python scripts?

00:18 Do you want to know how to structure your code files better so they’re more logical and more consistent? Do you generally want to organize your code in a better way? If so, this course is for you.

00:32 By the end of this course, you’ll know how to take your Python scripts and make them runnable, how to organize your Python scripts, where all the different pieces go, how to refactor your code to make future changes easier, and how to apply best practices to structure your script files.

00:50 All of these will get you closer to coding like Python software developers.

00:56 In this course, we will build a small application that generates a random motivational quote. I will show you what it looks like in action now. I will call python and this file called quote_generator.py and that will find us a random motivational quote and just print it to the console.

01:20 Now what we will also be able to do is run this .py file directly without the python command, and that will do the same thing. If you’re interested in that little trick of how to run a Python script without calling the python command directly, then the next lesson is for you.

Become a Member to join the conversation.