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
Copy file name to clipboardExpand all lines: source/exercises/mailroom/mailroom-pkg.rst
+6-2Lines changed: 6 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ The first step is to re-structure your code into separate files:
20
20
21
21
You should have all this pretty distinct after having refactored for the unit testing. If not, this is a good time to do it!
22
22
23
-
In addition to those three, you will want to write a top-level script file (probably called ``mailroom.py``) that does little but import the ui code and run a ``main()`` function. It should look something like this:
23
+
In addition to those three, you will want to write a top-level script file (perhaps called ``mailman.py``) that does little but import the ui code and run a ``main()`` function. It should look something like this:
24
24
25
25
.. code-block:: python
26
26
@@ -32,6 +32,8 @@ In addition to those three, you will want to write a top-level script file (prob
32
32
33
33
Yes, that's it! This has the advantage of keeping the top-level script really simple, as it has to get put somewhere else and it can keep the "real" code in the package where it belongs.
34
34
35
+
.. note:: Be careful here -- it is important not to call your top-level script the same thing as your package, in this case ``mailroom.py``. If you do, than when installed, python will find the script, rather than the package, when you do ``import mailroom``. You can call it ``mailroom`` without the Python, but that may confuse Windows.
36
+
35
37
36
38
Making the Package
37
39
------------------
@@ -49,7 +51,7 @@ Put all these in a python package structure, something like this::
49
51
test_model.py
50
52
test_cli.py
51
53
bin
52
-
mailroom.py
54
+
mailman.py
53
55
54
56
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)
55
57
@@ -63,6 +65,8 @@ To get the script installed you have two options. I prefer the more straightforw
63
65
64
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>`_
65
67
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.
Anaconda has seen a LOT of growth recently -- it's based on the open-source conda packaging system, and provides both a commercial curated set of packages, and a community-developed collection of packages known as conda-forge:
337
+
Conda has seen a LOT of growth in the last few years -- it's based on the open-source conda packaging system, and provides both a commercial curated set of packages, and a community-developed collection of packages known as conda-forge:
337
338
338
339
https://conda-forge.org/
339
340
@@ -548,7 +549,7 @@ You can find some additional notes here: :ref:`virtualenv_section`
548
549
Building Your Own Package
549
550
=========================
550
551
551
-
The term "package" is overloaded in Python. AS defined above, it means a collection of python modules. But it often is used to refer to not just the modules themselves, but the whole collection, with documentation and tests, bundled up and installable on other systems.
552
+
The term "package" is overloaded in Python. As defined above, it means a collection of python modules. But it often is used to refer to not just the modules themselves, but the whole collection, with documentation and tests, bundled up and installable on other systems.
552
553
553
554
Here are the very basics of what you need to know to make your own package.
description='An awesome package that does something',
@@ -745,6 +754,7 @@ An example:
745
754
],
746
755
)
747
756
757
+
748
758
``setup.cfg``
749
759
--------------
750
760
@@ -770,7 +780,7 @@ Note that an option spelled ``--foo-bar`` on the command-line is spelled f``foo_
770
780
Running ``setup.py``
771
781
--------------------
772
782
773
-
With a ``setup.py`` script defined, setuptools can do a lot:
783
+
With a ``setup.py`` script defined, setuptools, along with pip, can do a lot:
774
784
775
785
* builds a source distribution (a tar archive of all the files needed to build and install the package)::
776
786
@@ -790,6 +800,12 @@ With a ``setup.py`` script defined, setuptools can do a lot:
790
800
791
801
python setup.py install
792
802
803
+
or::
804
+
805
+
pip install .
806
+
807
+
(the dot means "this directory" -- pip will look in the current dir for a ``setup.py`` file)
808
+
793
809
* install in "develop" or "editable" mode::
794
810
795
811
python setup.py develop
@@ -798,6 +814,7 @@ or::
798
814
799
815
pip install -e .
800
816
817
+
.. note:: setuptools can be used by itself to build and install packages. But over the years, pip has evolved to a more "modern" way of doing things. When you install from source with pip -- it is using setuptools to do the work, but it changes things around, and installs things in a more modern, up to date, and compatible way. For much use, you won't notice the difference, but it setuptools still has some old crufty ways of doing things, so it's better to use pip as a front end as much as possible.
801
818
802
819
setuptools
803
820
-----------
@@ -816,17 +833,18 @@ This buys you a bunch of additional functionality:
816
833
* **develop mode**
817
834
* a LOT more
818
835
836
+
In fact, virtually all python packages use setuptools these days, and there is currently discussion of deprecating distutils, and making setuptools "official". So you really want to use it.
837
+
819
838
http://pythonhosted.org//setuptools/
820
839
821
840
wheels
822
-
-------
841
+
------
823
842
824
843
Wheels are a binary format for packages.
825
844
826
845
http://wheel.readthedocs.org/en/latest/
827
846
828
-
Pretty simple, essentially a zip archive of all the stuff that gets put
829
-
in ``site-packages``.
847
+
Pretty simple, essentially a zip archive of all the stuff that gets installed, i.e. put in ``site-packages``.
830
848
831
849
Can be just pure python or binary with compiled extensions
832
850
@@ -836,17 +854,9 @@ Building a wheel::
836
854
837
855
python setup.py bdist_wheel
838
856
839
-
Create a set of wheels (a wheelhouse)::
840
-
841
-
# Build a directory of wheels for pyramid and all its dependencies
The "easy" and traditional way to isntall command line scripts is with the ``scripts`` keyword argument to the ``setup()`` command::
1143
+
1144
+
1145
+
setup(...
1146
+
...
1147
+
scripts = ["bin/a_script.py"]
1148
+
...
1149
+
)
1150
+
1151
+
This works well on Unix systems (including the mac), but is not as reliable on Windows. All it really does is put a slightly altered copy of the script on PATH -- so it will work if it is named with the ``.py`` extension and the system is set up to run ``.py`` files.
1152
+
1153
+
entry points
1154
+
............
1155
+
1156
+
A more complicated, but better maintained and robust way is to use setuptools "entry points". Entry points can provide a number of functions, but one of them is to make console scripts. Also an argument to ``setup()``, It is done like so::
What this does is tell setuptools to make a little wrapper program called "script_name" that will start up python, and run the function called ``main`` in the package.module module.
0 commit comments