You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit bc79d77
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: _sources/exercises/mailroom/mailroom-pkg.rst.txt
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,8 +50,6 @@ Put all these in a python package structure, something like this::
50
50
__init__.py
51
51
test_model.py
52
52
test_cli.py
53
-
bin
54
-
mailman.py
55
53
56
54
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)
57
55
@@ -61,11 +59,13 @@ Now write your ``setup.py`` to support your package.
61
59
Making the top-level script runnable
62
60
------------------------------------
63
61
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>`_
65
63
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.
67
65
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:
Copy file name to clipboardExpand all lines: _sources/modules/Packaging.rst.txt
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1086,10 +1086,16 @@ I personally like the simplest one with the least magic:
1086
1086
...
1087
1087
)
1088
1088
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.
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.
1092
1096
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
+
1093
1099
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.
<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>
239
237
<p>Now write your <codeclass="docutils literal notranslate"><spanclass="pre">setup.py</span></code> to support your package.</p>
<h2>Making the top-level script runnable<aclass="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, <aclass="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 <codeclass="docutils literal notranslate"><spanclass="pre">setuptools</span></code>’s <aclass="reference external" href="http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point">entry points</a></p>
245
-
<divclass="admonition note">
246
-
<pclass="admonition-title">Note</p>
247
-
<p>On Unix systems, including the Mac, the simple <codeclass="docutils literal notranslate"><spanclass="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 <codeclass="docutils literal notranslate"><spanclass="pre">something.py</span></code> and that Windows is configured to run all files with <codeclass="docutils literal notranslate"><spanclass="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, <aclass="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 <codeclass="docutils literal notranslate"><spanclass="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 <codeclass="docutils literal notranslate"><spanclass="pre">something.py</span></code> and that Windows is configured to run all files with <codeclass="docutils literal notranslate"><spanclass="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>
<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>
<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
+
<divclass="admonition note">
1072
+
<pclass="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: <codeclass="docutils literal notranslate"><spanclass="pre">python</span><spanclass="pre">setup.py</span><spanclass="pre">build</span></code> that does just that. It will create a <codeclass="docutils literal notranslate"><spanclass="pre">build</span></code> directory, and in there, a <codeclass="docutils literal notranslate"><spanclass="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>
1069
1075
<p>Now you’ll need to write your code to find that data file. You can do that by using the <codeclass="docutils literal notranslate"><spanclass="pre">__file__</span></code> module attribute – then the location of the data file will be relative to the <codeclass="docutils literal notranslate"><spanclass="pre">__file__</span></code> that your code is in. A little massaging with a <codeclass="docutils literal notranslate"><spanclass="pre">pathlib.Path</span></code> should do it. Putting the path to the data directory in the package’s <codeclass="docutils literal notranslate"><spanclass="pre">__init__.py</span></code> provides a way for the rest of your code to find it.</p>
0 commit comments