From 6738b60dfe06d02394226918d64e01eb43657a6a Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 18 Sep 2023 11:14:54 +0200 Subject: [PATCH 1/2] Sample code for the article on running Python scripts --- run-python-scripts/README.md | 3 +++ run-python-scripts/hello.py | 3 +++ run-python-scripts/hello_shebang.py | 3 +++ run-python-scripts/run_exec.py | 2 ++ run-python-scripts/run_importlib.py | 2 ++ run-python-scripts/run_reload.py | 5 +++++ 6 files changed, 18 insertions(+) create mode 100644 run-python-scripts/README.md create mode 100644 run-python-scripts/hello.py create mode 100755 run-python-scripts/hello_shebang.py create mode 100644 run-python-scripts/run_exec.py create mode 100644 run-python-scripts/run_importlib.py create mode 100644 run-python-scripts/run_reload.py diff --git a/run-python-scripts/README.md b/run-python-scripts/README.md new file mode 100644 index 0000000000..e6ad64ee92 --- /dev/null +++ b/run-python-scripts/README.md @@ -0,0 +1,3 @@ +# How to Run Your Python Scripts and Code + +This folder provides the code examples for the Real Python tutorial [How to Run Your Python Scripts and Code](https://realpython.com/run-python-scripts/). diff --git a/run-python-scripts/hello.py b/run-python-scripts/hello.py new file mode 100644 index 0000000000..6e108ce1a0 --- /dev/null +++ b/run-python-scripts/hello.py @@ -0,0 +1,3 @@ +# hello.py + +print("Hello, World!") diff --git a/run-python-scripts/hello_shebang.py b/run-python-scripts/hello_shebang.py new file mode 100755 index 0000000000..01b85e848c --- /dev/null +++ b/run-python-scripts/hello_shebang.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3 + +print("Hello, World!") diff --git a/run-python-scripts/run_exec.py b/run-python-scripts/run_exec.py new file mode 100644 index 0000000000..345386e1a2 --- /dev/null +++ b/run-python-scripts/run_exec.py @@ -0,0 +1,2 @@ +with open("hello.py") as hello: + exec(hello.read()) diff --git a/run-python-scripts/run_importlib.py b/run-python-scripts/run_importlib.py new file mode 100644 index 0000000000..6e835920e0 --- /dev/null +++ b/run-python-scripts/run_importlib.py @@ -0,0 +1,2 @@ +import importlib +importlib.import_module('hello') diff --git a/run-python-scripts/run_reload.py b/run-python-scripts/run_reload.py new file mode 100644 index 0000000000..b184cfdd68 --- /dev/null +++ b/run-python-scripts/run_reload.py @@ -0,0 +1,5 @@ +import importlib + +import hello_shebang # First import. Second import does nothing + +importlib.reload(hello_shebang) From 21f4cc9d3fe3cca6856673a8b97a275cabd14187 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 18 Sep 2023 11:18:48 +0200 Subject: [PATCH 2/2] Fix linter issues --- run-python-scripts/run_importlib.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/run-python-scripts/run_importlib.py b/run-python-scripts/run_importlib.py index 6e835920e0..f34d9a498f 100644 --- a/run-python-scripts/run_importlib.py +++ b/run-python-scripts/run_importlib.py @@ -1,2 +1,3 @@ import importlib -importlib.import_module('hello') + +importlib.import_module("hello")