|
138 | 138 | "Move the `Point` class and the `distance` function to a new file called `point.py`:\n",
|
139 | 139 | "\n",
|
140 | 140 | "1. Create a new file called `point.py` in the same directory as this notebook.\n",
|
141 |
| - "2. Copy the `Point` class and the `distance` function to the new file.\n", |
| 141 | + "2. Copy **only** the `Point` class and the `distance` function to the new file.\n", |
142 | 142 | "3. Save the file."
|
143 | 143 | ]
|
144 | 144 | },
|
|
152 | 152 | {
|
153 | 153 | "cell_type": "code",
|
154 | 154 | "execution_count": null,
|
155 |
| - "metadata": {}, |
| 155 | + "metadata": { |
| 156 | + "tags": [] |
| 157 | + }, |
156 | 158 | "outputs": [],
|
157 | 159 | "source": [
|
158 | 160 | "from point import Point, distance\n",
|
|
188 | 190 | " import time\n",
|
189 | 191 | "\n",
|
190 | 192 | " print(\"I will now sleep\")\n",
|
191 |
| - " time.sleep(10)\n", |
| 193 | + " time.sleep(5)\n", |
192 | 194 | " print(\"I am now awake\")\n",
|
193 | 195 | "\n",
|
194 |
| - " def something() -> int:\n", |
| 196 | + " def some_function() -> int:\n", |
195 | 197 | " print(\"I don't do much\")\n",
|
196 | 198 | " ```\n",
|
197 | 199 | "\n",
|
|
202 | 204 | {
|
203 | 205 | "cell_type": "code",
|
204 | 206 | "execution_count": null,
|
205 |
| - "metadata": {}, |
| 207 | + "metadata": { |
| 208 | + "tags": [] |
| 209 | + }, |
206 | 210 | "outputs": [],
|
207 | 211 | "source": [
|
208 |
| - "from import_side_effect import something\n", |
209 |
| - "something()" |
| 212 | + "from import_side_effect import some_function\n", |
| 213 | + "some_function()" |
210 | 214 | ]
|
211 | 215 | },
|
212 | 216 | {
|
213 | 217 | "cell_type": "markdown",
|
214 | 218 | "metadata": {},
|
215 | 219 | "source": [
|
216 |
| - "As you see, importing the module printed a first message on the terminal, then waited some time and then finally printed a message on the terminal again. Only after this process finished you were able to execute `something`.\n", |
| 220 | + "As you see, importing the module printed a first message on the terminal, then waited some time and then finally printed a message on the terminal again.\n", |
| 221 | + "Only after this process finished you were able to execute `some_function`.\n", |
217 | 222 | "\n",
|
218 | 223 | "To avoid these problems, you should put these statements in a function that the user can call when they need it."
|
219 | 224 | ]
|
|
251 | 256 | {
|
252 | 257 | "cell_type": "code",
|
253 | 258 | "execution_count": null,
|
254 |
| - "metadata": {}, |
| 259 | + "metadata": { |
| 260 | + "tags": [] |
| 261 | + }, |
255 | 262 | "outputs": [],
|
256 | 263 | "source": [
|
257 | 264 | "from mypackage.point import Point, distance\n",
|
|
289 | 296 | {
|
290 | 297 | "cell_type": "code",
|
291 | 298 | "execution_count": null,
|
292 |
| - "metadata": {}, |
| 299 | + "metadata": { |
| 300 | + "tags": [] |
| 301 | + }, |
293 | 302 | "outputs": [],
|
294 | 303 | "source": [
|
295 | 304 | "from mypackage.point import Point\n",
|
|
318 | 327 | "Let's see some examples:"
|
319 | 328 | ]
|
320 | 329 | },
|
| 330 | + { |
| 331 | + "cell_type": "markdown", |
| 332 | + "metadata": {}, |
| 333 | + "source": [ |
| 334 | + "**Same as above:**" |
| 335 | + ] |
| 336 | + }, |
321 | 337 | {
|
322 | 338 | "cell_type": "code",
|
323 | 339 | "execution_count": null,
|
324 |
| - "metadata": {}, |
| 340 | + "metadata": { |
| 341 | + "tags": [] |
| 342 | + }, |
325 | 343 | "outputs": [],
|
326 | 344 | "source": [
|
327 |
| - "# Same as above\n", |
328 | 345 | "from mypackage.point import Point\n",
|
329 | 346 | "from mypackage.utils import distance\n",
|
330 | 347 | "\n",
|
|
333 | 350 | "print(f\"The distance between `p1` and `p2` is {distance(p1, p2)}\")"
|
334 | 351 | ]
|
335 | 352 | },
|
| 353 | + { |
| 354 | + "cell_type": "markdown", |
| 355 | + "metadata": {}, |
| 356 | + "source": [ |
| 357 | + "**Import only the `mypackage` package:**" |
| 358 | + ] |
| 359 | + }, |
336 | 360 | {
|
337 | 361 | "cell_type": "code",
|
338 | 362 | "execution_count": null,
|
339 |
| - "metadata": {}, |
| 363 | + "metadata": { |
| 364 | + "tags": [] |
| 365 | + }, |
340 | 366 | "outputs": [],
|
341 | 367 | "source": [
|
342 |
| - "# Import only the `mypackage` package\n", |
343 | 368 | "import mypackage\n",
|
344 | 369 | "\n",
|
345 | 370 | "p1 = mypackage.point.Point(0, 0)\n",
|
|
348 | 373 | "print(f\"The distance between `p1` and `p2` is {mypackage.utils.distance(p1, p2)}\")"
|
349 | 374 | ]
|
350 | 375 | },
|
| 376 | + { |
| 377 | + "cell_type": "markdown", |
| 378 | + "metadata": { |
| 379 | + "tags": [] |
| 380 | + }, |
| 381 | + "source": [ |
| 382 | + "**Import `point` and `utils` modules from `mypackage`:**" |
| 383 | + ] |
| 384 | + }, |
351 | 385 | {
|
352 | 386 | "cell_type": "code",
|
353 | 387 | "execution_count": null,
|
354 |
| - "metadata": {}, |
| 388 | + "metadata": { |
| 389 | + "tags": [] |
| 390 | + }, |
355 | 391 | "outputs": [],
|
356 | 392 | "source": [
|
357 |
| - "# Import `point` and `utils` modules from `mypackage`\n", |
358 | 393 | "from mypackage import point, utils\n",
|
359 | 394 | "\n",
|
360 | 395 | "p1 = point.Point(0, 0)\n",
|
|
417 | 452 | "If everything went well, we should be able to execute the following code:"
|
418 | 453 | ]
|
419 | 454 | },
|
| 455 | + { |
| 456 | + "cell_type": "markdown", |
| 457 | + "metadata": {}, |
| 458 | + "source": [ |
| 459 | + "<div class=\"alert alert-block alert-warning\">\n", |
| 460 | + " <b> Warning: </b> Sometimes for the code below to work you need to restart the Python kernel. Find the <b>Kernel</b> menu at the top and then choose <b>Restart Kernel</b>.\n", |
| 461 | + "The reason for this is that Python has imported the package before, and it is too lazy to import it again.\n", |
| 462 | + "</div>" |
| 463 | + ] |
| 464 | + }, |
420 | 465 | {
|
421 | 466 | "cell_type": "code",
|
422 | 467 | "execution_count": null,
|
423 |
| - "metadata": {}, |
| 468 | + "metadata": { |
| 469 | + "tags": [] |
| 470 | + }, |
424 | 471 | "outputs": [],
|
425 | 472 | "source": [
|
426 | 473 | "from mypackage import Point, distance\n",
|
|
430 | 477 | "print(f\"The distance between `p1` and `p2` is {distance(p1, p2)}\")"
|
431 | 478 | ]
|
432 | 479 | },
|
433 |
| - { |
434 |
| - "cell_type": "markdown", |
435 |
| - "metadata": {}, |
436 |
| - "source": [ |
437 |
| - "<div class=\"alert alert-block alert-warning\">\n", |
438 |
| - " <b> Warning: </b> If you did everything correctly but nothing works, consider restarting the Python kernel. Find the <b>Kernel</b> menu and then choose <b>Restart Kernel</b>.\n", |
439 |
| - "Python has imported the package before, and it is too lazy to import it again.\n", |
440 |
| - "We need to restart Python.\n", |
441 |
| - "</div>" |
442 |
| - ] |
443 |
| - }, |
444 | 480 | {
|
445 | 481 | "cell_type": "markdown",
|
446 | 482 | "metadata": {},
|
|
453 | 489 | {
|
454 | 490 | "cell_type": "code",
|
455 | 491 | "execution_count": null,
|
456 |
| - "metadata": {}, |
| 492 | + "metadata": { |
| 493 | + "tags": [] |
| 494 | + }, |
457 | 495 | "outputs": [],
|
458 | 496 | "source": [
|
459 | 497 | "import mypackage\n",
|
|
474 | 512 | {
|
475 | 513 | "cell_type": "code",
|
476 | 514 | "execution_count": null,
|
477 |
| - "metadata": {}, |
| 515 | + "metadata": { |
| 516 | + "tags": [] |
| 517 | + }, |
478 | 518 | "outputs": [],
|
479 | 519 | "source": [
|
480 | 520 | "import mypackage as mp\n",
|
|
522 | 562 | {
|
523 | 563 | "cell_type": "code",
|
524 | 564 | "execution_count": null,
|
525 |
| - "metadata": {}, |
| 565 | + "metadata": { |
| 566 | + "tags": [] |
| 567 | + }, |
526 | 568 | "outputs": [],
|
527 | 569 | "source": [
|
528 | 570 | "class Line:\n",
|
|
562 | 604 | {
|
563 | 605 | "cell_type": "code",
|
564 | 606 | "execution_count": null,
|
565 |
| - "metadata": {}, |
| 607 | + "metadata": { |
| 608 | + "tags": [] |
| 609 | + }, |
566 | 610 | "outputs": [],
|
567 | 611 | "source": [
|
568 | 612 | "import mypackage as mp\n",
|
|
609 | 653 | {
|
610 | 654 | "cell_type": "code",
|
611 | 655 | "execution_count": null,
|
612 |
| - "metadata": {}, |
| 656 | + "metadata": { |
| 657 | + "tags": [] |
| 658 | + }, |
613 | 659 | "outputs": [],
|
614 | 660 | "source": [
|
615 | 661 | "import mypackage as mp\n",
|
|
659 | 705 | {
|
660 | 706 | "cell_type": "code",
|
661 | 707 | "execution_count": null,
|
662 |
| - "metadata&q
2851
uot;: {}, |
| 708 | + "metadata": { |
| 709 | + "tags": [] |
| 710 | + }, |
663 | 711 | "outputs": [],
|
664 | 712 | "source": [
|
665 | 713 | "import mypackage as mp\n",
|
|
779 | 827 | "## Exercise on installing the package\n",
|
780 | 828 | "\n",
|
781 | 829 | "1. Inside the `mypackage` directory, we need to create another directory called `mypackage`.\n",
|
782 |
| - "2. Move the `geometry` directory together with `__init__.py` and `utils.py` files to the `mypackage` sub-directory.\n", |
| 830 | + "2. Move the `geometry` directory together with `__init__.py` and `utils.py` files to the freshly-created `mypackage` sub-directory.\n", |
783 | 831 | "3. Create a new file called `pyproject.toml` in the top-level `mypackage` directory with the content from the cell above.\n",
|
784 |
| - "4. Open the terminal (`File` -> `New Launcher`, then select `Terminal`), and navigate to the `mypackage` directory.\n", |
| 832 | + "4. Open the terminal (`File` -> `New Launcher`, then select `Terminal`), and run the following command to install `mypackage`:\n", |
785 | 833 | "```bash\n",
|
786 |
| - " $ cd mypackage\n", |
| 834 | + " $ pip install mypackage/\n", |
787 | 835 | "```\n",
|
788 |
| - "5. Run the following command to install the package:\n", |
789 |
| - "```bash\n", |
790 |
| - " $ pip install .\n", |
791 |
| - "```\n", |
792 |
| - "6. Return to the new notebook we created in the previous exercise and try to import the `mypackage` package again.\n", |
| 836 | + "5. Return to the new notebook we created in the previous exercise and try to import the `mypackage` package again.\n", |
793 | 837 | "\n",
|
794 | 838 | "\n",
|
795 | 839 | "Congratulations, now your package is installed and you can import it from anywhere!"
|
|
999 | 1043 | "name": "python",
|
1000 | 1044 | "nbconvert_exporter": "python",
|
1001 | 1045 | "pygments_lexer": "ipython3",
|
1002 |
| - "version": "3.9.6" |
1003 |
| - }, |
1004 |
| - "vscode": { |
1005 |
| - "interpreter": { |
1006 |
| - "hash": "2b2b08c237e7c74caf82ad6490346743fce9bd812859047e307c1031e538ab1c" |
1007 |
| - } |
| 1046 | + "version": "3.9.12" |
1008 | 1047 | }
|
1009 | 1048 | },
|
1010 | 1049 | "nbformat": 4,
|
|
0 commit comments