|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Intereacting with the shell" |
| 7 | + "# Interacting with the shell" |
8 | 8 | ]
|
9 | 9 | },
|
10 | 10 | {
|
|
18 | 18 | "cell_type": "markdown",
|
19 | 19 | "metadata": {},
|
20 | 20 | "source": [
|
21 |
| - "The [`sh` module](https://amoffat.github.io/sh/) is very convenient to interact with the shell. Note that `sh` is not part of Python's standard library, if you prefer not to use extra modules, use the `subprocess` module in the standard library." |
| 21 | + "The [`sh` module](https://amoffat.github.io/sh/) is very convenient to interact with the shell. Note that `sh` is not part of Python's standard library, if you prefer not to use extra modules, use the `subprocess` module in the standard library. The statements below will install `sh` using `pip` is it isn't already installed." |
22 | 22 | ]
|
23 | 23 | },
|
24 | 24 | {
|
|
27 | 27 | "metadata": {},
|
28 | 28 | "outputs": [],
|
29 | 29 | "source": [
|
30 |
| - "!pip install sh" |
31 |
| - ] |
32 |
| - }, |
33 |
| - { |
34 |
| - "cell_type": "code", |
35 |
| - "execution_count": null, |
36 |
| - "metadata": {}, |
37 |
| - "outputs": [], |
38 |
| - "source": [ |
39 |
| - "import sh" |
| 30 | + "try:\n", |
| 31 | + " import sh\n", |
| 32 | + "except ModuleNotFoundError:\n", |
| 33 | + " print('installing sh using pip')\n", |
| 34 | + " !pip install sh\n", |
| 35 | + " import sh" |
40 | 36 | ]
|
41 | 37 | },
|
42 | 38 | {
|
|
314 | 310 | {
|
315 | 311 | "cell_type": "code",
|
316 | 312 | "execution_count": null,
|
317 |
| - "metadata": {}, |
| 313 | + "metadata": { |
| 314 | + "tags": [] |
| 315 | + }, |
318 | 316 | "outputs": [],
|
319 | 317 | "source": [
|
320 | 318 | "try:\n",
|
|
323 | 321 | " print(error)"
|
324 | 322 | ]
|
325 | 323 | },
|
| 324 | + { |
| 325 | + "cell_type": "markdown", |
| 326 | + "metadata": {}, |
| 327 | + "source": [ |
| 328 | + "### Clean up" |
| 329 | + ] |
| 330 | + }, |
| 331 | + { |
| 332 | + "cell_type": "markdown", |
| 333 | + "metadata": {}, |
| 334 | + "source": [ |
| 335 | + "Remove the `tmp` directory." |
| 336 | + ] |
| 337 | + }, |
| 338 | + { |
| 339 | + "cell_type": "code", |
| 340 | + "execution_count": null, |
| 341 | + "metadata": {}, |
| 342 | + "outputs": [], |
| 343 | + "source": [ |
| 344 | + "sh.rm('-rf', 'tmp')" |
| 345 | + ] |
| 346 | + }, |
326 | 347 | {
|
327 | 348 | "cell_type": "markdown",
|
328 | 349 | "metadata": {},
|
|
605 | 626 | " env=environ, shell=True)\n",
|
606 | 627 | "print(process.stdout.rstrip())"
|
607 | 628 | ]
|
| 629 | + }, |
| 630 | + { |
| 631 | + "cell_type": "markdown", |
| 632 | + "metadata": {}, |
| 633 | + "source": [ |
| 634 | + "### Clean up" |
| 635 | + ] |
| 636 | + }, |
| 637 | + { |
| 638 | + "cell_type": "markdown", |
| 639 | + "metadata": {}, |
| 640 | + "source": [ |
| 641 | + "Remove the `tmp` directory." |
| 642 | + ] |
| 643 | + }, |
| 644 | + { |
| 645 | + "cell_type": "code", |
| 646 | + "execution_count": null, |
| 647 | + "metadata": {}, |
| 648 | + "outputs": [], |
| 649 | + "source": [ |
| 650 | + "process = subprocess.run(['rm', '-rf', 'tmp'])" |
| 651 | + ] |
| 652 | + }, |
| 653 | + { |
| 654 | + "cell_type": "code", |
| 655 | + "execution_count": null, |
| 656 | + "metadata": {}, |
| 657 | + "outputs": [], |
| 658 | + "source": [ |
| 659 | + "process.returncode" |
| 660 | + ] |
608 | 661 | }
|
609 | 662 | ],
|
610 | 663 | "metadata": {
|
611 | 664 | "kernelspec": {
|
612 |
| - "display_name": "Python 3", |
| 665 | + "display_name": "Python 3 (ipykernel)", |
613 | 666 | "language": "python",
|
614 | 667 | "name": "python3"
|
615 | 668 | },
|
|
623 | 676 | "name": "python",
|
624 | 677 | "nbconvert_exporter": "python",
|
625 | 678 | "pygments_lexer": "ipython3",
|
626 |
| - "version": "3.7.5" |
| 679 | + "version": "3.11.3" |
627 | 680 | }
|
628 | 681 | },
|
629 | 682 | "nbformat": 4,
|
630 |
| - "nbformat_minor": 2 |
| 683 | + "nbformat_minor": 4 |
631 | 684 | }
|
0 commit comments