|
198 | 198 | "- [Context Manager](#context-manager)\n", |
199 | 199 | " - [with statement](#with-statement)\n", |
200 | 200 | " - [Writing your own contextmanager using generator syntax](#writing-your-own-contextmanager-using-generator-syntax)\n", |
201 | | -"- [__main__ Top-level script environment](#main-top-level-script-environment)\n", |
| 201 | +"- [`__main__` Top-level script environment](#main-top-level-script-environment)\n", |
202 | 202 | " - [Advantages](#advantages)\n", |
| 203 | +"- [setup.py](#setuppy)\n", |
203 | 204 | "- [Virtual Environment](#virtual-environment)\n", |
204 | 205 | " - [virtualenv](#virtualenv)\n", |
205 | 206 | " - [pipenv](#pipenv)\n", |
|
7450 | 7451 | "source": [ |
7451 | 7452 | "[*Return to the Top*](#python-cheatsheet)\n", |
7452 | 7453 | "\n", |
7453 | | -"## __main__ Top-level script environment\n", |
| 7454 | +"##`__main__` Top-level script environment\n", |
7454 | 7455 | "\n", |
7455 | 7456 | "`__main__` is the name of the scope in which top-level code executes.\n", |
7456 | 7457 | "A module’s __name__ is set equal to `__main__` when read from standard input, a script, or from an interactive prompt.\n", |
|
7513 | 7514 | "\n", |
7514 | 7515 | "[*Return to the Top*](#python-cheatsheet)\n", |
7515 | 7516 | "\n", |
| 7517 | +"## setup.py\n", |
| 7518 | +"\n", |
| 7519 | +"The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing.\n", |
| 7520 | +"\n", |
| 7521 | +"The `setup.py` file is at the heart of a Python project. It describes all of the metadata about your project. There a quite a few fields you can add to a project to give it a rich set of metadata describing the project. However, there are only three required fields: name, version, and packages. The name field must be unique if you wish to publish your package on the Python Package Index (PyPI). The version field keeps track of different releases of the project. The packages field describes where you’ve put the Python source code within your project.\n", |
| 7522 | +"\n", |
| 7523 | +"This allows you to easily install Python packages. Often it's enough to write:" |
| 7524 | + ] |
| 7525 | + }, |
| 7526 | + { |
| 7527 | +"cell_type":"code", |
| 7528 | +"execution_count":null, |
| 7529 | +"metadata": {}, |
| 7530 | +"outputs": [], |
| 7531 | +"source": [ |
| 7532 | +"%%bash\n", |
| 7533 | +"python setup.py install" |
| 7534 | + ] |
| 7535 | + }, |
| 7536 | + { |
| 7537 | +"cell_type":"markdown", |
| 7538 | +"metadata": {}, |
| 7539 | +"source": [ |
| 7540 | +"and module will install itself.\n", |
| 7541 | +"\n", |
| 7542 | +"Our initial setup.py will also include information about the license and will re-use the README.txt file for the long_description field. This will look like:" |
| 7543 | + ] |
| 7544 | + }, |
| 7545 | + { |
| 7546 | +"cell_type":"code", |
| 7547 | +"execution_count":null, |
| 7548 | +"metadata": {}, |
| 7549 | +"outputs": [], |
| 7550 | +"source": [ |
| 7551 | +">>> from distutils.core import setup\n", |
| 7552 | +">>> setup(\n", |
| 7553 | +"... name='pythonCheatsheet',\n", |
| 7554 | +"... version='0.1',\n", |
| 7555 | +"... packages=['pipenv',],\n", |
| 7556 | +"... license='MIT',\n", |
| 7557 | +"... long_description=open('README.txt').read(),\n", |
| 7558 | +"... )" |
| 7559 | + ] |
| 7560 | + }, |
| 7561 | + { |
| 7562 | +"cell_type":"markdown", |
| 7563 | +"metadata": {}, |
| 7564 | +"source": [ |
| 7565 | +"Find more information visit [http://docs.python.org/install/index.html](http://docs.python.org/install/index.html).\n", |
| 7566 | +"\n", |
| 7567 | +"[*Return to the Top*](#python-cheatsheet)\n", |
| 7568 | +"\n", |
7516 | 7569 | "## Virtual Environment\n", |
7517 | 7570 | "\n", |
7518 | 7571 | "The use of a Virtual Environment is to test python code in encapsulated environments and to also avoid filling the base Python installation with libraries we might use for only one project.\n", |
|