Skip to content

Commit bc79d77

Browse files
committed
updating rendered materials
1 parent 0d96230 commit bc79d77

5 files changed

Lines changed: 22 additions & 14 deletions

File tree

‎_sources/exercises/mailroom/mailroom-pkg.rst.txt‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ Put all these in a python package structure, something like this::
5050
__init__.py
5151
test_model.py
5252
test_cli.py
53-
bin
54-
mailman.py
5553

5654
You will need to import the logic code from model.py in the cli code in order to use it. You can wait until you learn about mocking to write the code in test_cli.py (so you can leave that out)
5755

@@ -61,11 +59,13 @@ Now write your ``setup.py`` to support your package.
6159
Making the top-level script runnable
6260
------------------------------------
6361

64-
To get the script installed you have two options. I prefer the more straightforward one, `the scripts keyword argument <http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-scripts-keyword-argument>`_
62+
To get the script installed you have two options. I used to prefer the more straightforward one, `the scripts keyword argument <http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-scripts-keyword-argument>`_
6563

66-
But if you want to get fancy, you can use ``setuptools``'s `entry points <http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point>`_
64+
But it turns out that while the simple ``scripts`` keyword argument method works well and is simple, it may not work as well on Windows -- it relies in your script being named ``something.py`` and that Windows is configured to run all files with ``.py`` extensions. Not all windows systems are set up this way. But the "entry points" method builds a little exe file to call your script, so it's more reliable.
6765

68-
.. note:: On Unix systems, including the Mac, the simple ``scripts`` keyword argument method works well and is simple. But it may not work as well on Windows -- it relies in your script being named ``something.py`` and that Windows is configured to run all files with ``.py`` extensions. Not all windows systems are set up this way. But the "entry points" method builds a little exe file to call your script, so it's more reliable.
66+
And the Python community has moved very much towards using setuptools entry points, so That's really the way to go now:
67+
68+
http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point
6969

7070

7171
Including data files

‎_sources/modules/Packaging.rst.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,10 +1086,16 @@ I personally like the simplest one with the least magic:
10861086
...
10871087
)
10881088
1089+
This is a dict with the keys being the package(s) you want to add data files to. This is required, as a single setup command can install more than one package. The value(s) is a list of filenames, *relative to the package* - note that in the above example, the "pkg_name" is not part of the path to the file.
1090+
1091+
WARNING: For some reason, setuptools does not give you an error or warning if it can't find the files you specify -- which is a real shame - makes it harder to debug.
1092+
10891093
https://packaging.python.org/tutorials/distributing-packages/#package-data
10901094

10911095
Then you'll get the data file included in the package in the same place relative to your code regardless of how (or whether) it is installed.
10921096

1097+
.. note:: Debugging package building can be kind of tricky: if you install the package, and it doesn't work, what went wrong?!? One approach that can help is to "build" the package, separately from installing it. setuptools provides a build command: ``python setup.py build`` that does just that. It will create a ``build`` directory, and in there, a ``lib`` dir. In there is what will actually get installed -- your "built" package. So you can look there and see if your data files are getting included, and everything else about the package.
1098+
10931099
Now you'll need to write your code to find that data file. You can do that by using the ``__file__`` module attribute -- then the location of the data file will be relative to the ``__file__`` that your code is in. A little massaging with a ``pathlib.Path`` should do it. Putting the path to the data directory in the package's ``__init__.py`` provides a way for the rest of your code to find it.
10941100

10951101
In ``pkg_name/__init__.py``:

‎exercises/mailroom/mailroom-pkg.html‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,17 @@ <h2>Making the Package<a class="headerlink" href="#making-the-package" title="Pe
231231
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
232232
<span class="n">test_model</span><span class="o">.</span><span class="n">py</span>
233233
<span class="n">test_cli</span><span class="o">.</span><span class="n">py</span>
234-
<span class="nb">bin</span>
235-
<span class="n">mailman</span><span class="o">.</span><span class="n">py</span>
236234
</pre></div>
237235
</div>
238236
<p>You will need to import the logic code from model.py in the cli code in order to use it. You can wait until you learn about mocking to write the code in test_cli.py (so you can leave that out)</p>
239237
<p>Now write your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> to support your package.</p>
240238
</div>
241239
<div class="section" id="making-the-top-level-script-runnable">
242240
<h2>Making the top-level script runnable<a class="headerlink" href="#making-the-top-level-script-runnable" title="Permalink to this headline"></a></h2>
243-
<p>To get the script installed you have two options. I prefer the more straightforward one, <a class="reference external" href="http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-scripts-keyword-argument">the scripts keyword argument</a></p>
244-
<p>But if you want to get fancy, you can use <code class="docutils literal notranslate"><span class="pre">setuptools</span></code>’s <a class="reference external" href="http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point">entry points</a></p>
245-
<div class="admonition note">
246-
<p class="admonition-title">Note</p>
247-
<p>On Unix systems, including the Mac, the simple <code class="docutils literal notranslate"><span class="pre">scripts</span></code> keyword argument method works well and is simple. But it may not work as well on Windows – it relies in your script being named <code class="docutils literal notranslate"><span class="pre">something.py</span></code> and that Windows is configured to run all files with <code class="docutils literal notranslate"><span class="pre">.py</span></code> extensions. Not all windows systems are set up this way. But the “entry points” method builds a little exe file to call your script, so it’s more reliable.</p>
248-
</div>
241+
<p>To get the script installed you have two options. I used to prefer the more straightforward one, <a class="reference external" href="http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-scripts-keyword-argument">the scripts keyword argument</a></p>
242+
<p>But it turns out that while the simple <code class="docutils literal notranslate"><span class="pre">scripts</span></code> keyword argument method works well and is simple, it may not work as well on Windows – it relies in your script being named <code class="docutils literal notranslate"><span class="pre">something.py</span></code> and that Windows is configured to run all files with <code class="docutils literal notranslate"><span class="pre">.py</span></code> extensions. Not all windows systems are set up this way. But the “entry points” method builds a little exe file to call your script, so it’s more reliable.</p>
243+
<p>And the Python community has moved very much towards using setuptools entry points, so That’s really the way to go now:</p>
244+
<p><a class="reference external" href="http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point">http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point</a></p>
249245
</div>
250246
<div class="section" id="including-data-files">
251247
<h2>Including data files<a class="headerlink" href="#including-data-files" title="Permalink to this headline"></a></h2>

‎modules/Packaging.html‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,14 @@ <h4>The simplest option: package_data<a class="headerlink" href="#the-simplest-o
10641064
<span class="p">)</span>
10651065
</pre></div>
10661066
</div>
1067+
<p>This is a dict with the keys being the package(s) you want to add data files to. This is required, as a single setup command can install more than one package. The value(s) is a list of filenames, <em>relative to the package</em> - note that in the above example, the “pkg_name” is not part of the path to the file.</p>
1068+
<p>WARNING: For some reason, setuptools does not give you an error or warning if it can’t find the files you specify – which is a real shame - makes it harder to debug.</p>
10671069
<p><a class="reference external" href="https://packaging.python.org/tutorials/distributing-packages/#package-data">https://packaging.python.org/tutorials/distributing-packages/#package-data</a></p>
10681070
<p>Then you’ll get the data file included in the package in the same place relative to your code regardless of how (or whether) it is installed.</p>
1071+
<div class="admonition note">
1072+
<p class="admonition-title">Note</p>
1073+
<p>Debugging package building can be kind of tricky: if you install the package, and it doesn’t work, what went wrong?!? One approach that can help is to “build” the package, separately from installing it. setuptools provides a build command: <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">build</span></code> that does just that. It will create a <code class="docutils literal notranslate"><span class="pre">build</span></code> directory, and in there, a <code class="docutils literal notranslate"><span class="pre">lib</span></code> dir. In there is what will actually get installed – your “built” package. So you can look there and see if your data files are getting included, and everything else about the package.</p>
1074+
</div>
10691075
<p>Now you’ll need to write your code to find that data file. You can do that by using the <code class="docutils literal notranslate"><span class="pre">__file__</span></code> module attribute – then the location of the data file will be relative to the <code class="docutils literal notranslate"><span class="pre">__file__</span></code> that your code is in. A little massaging with a <code class="docutils literal notranslate"><span class="pre">pathlib.Path</span></code> should do it. Putting the path to the data directory in the package’s <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> provides a way for the rest of your code to find it.</p>
10701076
<p>In <code class="docutils literal notranslate"><span class="pre">pkg_name/__init__.py</span></code>:</p>
10711077
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>

‎searchindex.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)