|
18 | 18 | "\n", |
19 | 19 | "Despite our regular use of the `import` statement, we have thus far swept its details under the rug. Here, we will finally pay our due diligence and discuss Python's import system, which entails understanding the way that code can be organized into modules and packages. Modules are individual `.py` files from which we can import functions and objects, and packages are collections of such modules. Detailing this packaging system will not only provide us with insight into the organization of the standard library and other collections of Python code, but it will permit us to create our own packages of code.\n", |
20 | 20 | "\n", |
21 | | - "To conclude this section, we will demonstrate the process of installing a Python package to your system; supposing that you have written your own Python package, installing it enables you to import it anywhere on your system. A brief overview will be provided of the two most popular venues for hosting Python packages to the world at large: the Python Package Index (PyPI) and Anaconda.org.\n", |
| 21 | + "To conclude this section, we will demonstrate the process of installing a Python package on your system; supposing that you have written your own Python package, installing it enables you to import it anywhere on your system. A brief overview will be provided of the two most popular venues for hosting Python packages to the world at large: the Python Package Index (PyPI) and Anaconda.org.\n", |
22 | 22 | "\n", |
23 | 23 | "The [official Python tutorial](https://docs.python.org/3/tutorial/modules.html) provides a nice overview of this material and dives into details that we will not concern ourselves with here. " |
24 | 24 | ] |
|
49 | 49 | "source": [ |
50 | 50 | "## Modules\n", |
51 | 51 | "\n", |
52 | | - "A Python module refers to a single `.py` file that contains function definitions and variable-assignment statements. Importing a module will execute these statements, rendering the resulting objects available via the imported module. \n", |
| 52 | + "A Python 'module' refers to a single `.py` file that contains function definitions and variable-assignment statements. Importing a module will execute these statements, rendering the resulting objects available via the imported module. \n", |
53 | 53 | "\n", |
54 | | - "Let's create our own module and import it into an interactive Python session. Open a Jupyter notebook or IPython console in a known directory on your computer. Now, with an [IDE](http://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Getting_Started_With_IDEs_and_Notebooks.html#Integrated-Development-Environments) or simple text editor (not software like Microsoft Word!) create text file named `my_module.py` in the same directory as you Python session. The contents of `my_module.py` should be:\n", |
| 54 | + "Let's create our own module and import it into an interactive Python session. Open a Jupyter notebook or IPython console in a known directory on your computer. Now, with an [IDE](http://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Getting_Started_With_IDEs_and_Notebooks.html#Integrated-Development-Environments) or simple text editor (not software like Microsoft Word!) create a text file named `my_module.py` in the same directory as you Python session. The contents of `my_module.py` should be:\n", |
55 | 55 | "\n", |
56 | 56 | "```python\n", |
57 | 57 | "\"\"\"\n", |
|
401 | 401 | "\n", |
402 | 402 | "### PYTHONPATH and Site-Packages\n", |
403 | 403 | "\n", |
404 | | - "Thus far in our reading of Python packages we have had to take care that all of the modules and packages that we have written reside in the same directory as our interactive Python session. How is it that we are able to import NumPy in any session or module, without any knowledge of where that package is located? This is because we have installed NumPy, which means that the package has been placed in our \"Python path\", which is indicated as PYTHONPATH.\n", |
| 404 | + "Thus far in our reading of Python packages we have had to take care that all of the modules and packages that we have written reside in the same directory as our interactive Python session. How is it that we are able to import NumPy in any session or module, without any knowledge of where that package is located? This is because we have installed *NumPy*, which means that the package has been placed in our \"Python path\", which is indicated as PYTHONPATH.\n", |
405 | 405 | "\n", |
406 | | - "The PYTHONPATH specifies the directories that the Python interpreter will look to when importing modules. You can check your PYTHONPATH in `sys.path`:\n", |
| 406 | + "The PYTHONPATH specifies the directories that the Python interpreter will look in when importing modules. You can check your PYTHONPATH using `sys.path`:\n", |
407 | 407 | "\n", |
408 | 408 | "```python\n", |
409 | 409 | "# looking up PYTHONPATH\n", |
|
486 | 486 | "source": [ |
487 | 487 | "If you read through the additional materials linked above, you will see that there are many more fields of optional information that can be provided in this setup script, such as the author name, any installation requirements that the package has, and more.\n", |
488 | 488 | "\n", |
489 | | - "Armed with this script, we are ready to install our package locally on our machine! In you terminal, navigate to the directory containing this setup script and your package that it being installed. Run\n", |
| 489 | + "Armed with this script, we are ready to install our package locally on our machine! In your terminal, navigate to the directory containing this setup script and your package that it being installed. Run\n", |
490 | 490 | "\n", |
491 | 491 | "```shell\n", |
492 | 492 | "python setup.py install\n", |
|
527 | 527 | "\n", |
528 | 528 | "Both managers will install packages to your site-packages directory.\n", |
529 | 529 | "\n", |
530 | | - "There are substantial benefits for using `conda` rather than to install packages. First and foremost, `conda` has a powerful \"environment solver\", which tracks the inter-dependencies of Python packages. Thus it will attempt to install, upgrade, and downgrade packages as needed to accommodate your installations. Additionally, the default list of packages available via `conda` are curated and maintained by Continuum Analytics, the creators of Anaconda. To elucidate one of the benefits of this, installing NumPy via `pip` will deliver the vanilla version of NumPy to you, `conda` will install an [mkl-optimized version of NumPy](https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl), which can execute routines substantially faster. Finally, `conda` also serves as [an environment manager](https://conda.io/docs/user-guide/tasks/manage-environments.html), which allows you to maintain multiple, non-conflicting environments that can house different configurations of installed Python packages and even different versions of Python itself.\n", |
| 530 | + "There are substantial benefits for using `conda` rather than `pip` to install packages. First and foremost, `conda` has a powerful \"environment solver\", which tracks the inter-dependencies of Python packages. Thus it will attempt to install, upgrade, and downgrade packages as needed to accommodate your installations. Additionally, the default list of packages available via `conda` are curated and maintained by Continuum Analytics, the creators of Anaconda. To elucidate one of the benefits of this, installing NumPy via `pip` will deliver the vanilla version of NumPy to you; `conda` will install an [mkl-optimized version of NumPy](https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl), which can execute routines substantially faster. Finally, `conda` also serves as [an environment manager](https://conda.io/docs/user-guide/tasks/manage-environments.html), which allows you to maintain multiple, non-conflicting environments that can house different configurations of installed Python packages and even different versions of Python itself.\n", |
531 | 531 | "\n", |
532 | 532 | "That being said, there are some benefits to using `pip`. PyPi is accessible and easy to upload packages to; this is likely the easiest means for distributing a Python package worldwide. As such, `pip` provides access to a wider range of Python packages. That being said, `conda` can also be directed to install packages from custom channels - providing access to packages outside of the curated Anaconda distribution. This has become a popular method of installation for machine learning libraries like PyTorch and TensorFlow." |
533 | 533 | ] |
534 | 534 | }, |
| 535 | + { |
| 536 | + "cell_type": "markdown", |
| 537 | + "metadata": {}, |
| 538 | + "source": [ |
| 539 | + "## Links to Official Documentation\n", |
| 540 | + "\n", |
| 541 | + "- [Python Tutorial: Modules](https://docs.python.org/3/tutorial/modules.html)\n", |
| 542 | + "- [An Introduction to Distutils](https://docs.python.org/3/distutils/introduction.html#an-introduction-to-distutils)\n", |
| 543 | + "- [Packaging Your Project](https://packaging.python.org/tutorials/packaging-projects/#packaging-your-project) \n", |
| 544 | + "- [PyPi](https://pypi.org/)" |
| 545 | + ] |
| 546 | + }, |
535 | 547 | { |
536 | 548 | "cell_type": "markdown", |
537 | 549 | "metadata": {}, |
|
617 | 629 | "name": "python", |
618 | 630 | "nbconvert_exporter": "python", |
619 | 631 | "pygments_lexer": "ipython3", |
620 | | - "version": "3.6.5" |
| 632 | + "version": "3.6.3" |
621 | 633 | } |
622 | 634 | }, |
623 | 635 | "nbformat": 4, |
|
0 commit comments