{"id":2239,"date":"2020-09-18T23:26:46","date_gmt":"2020-09-19T06:26:46","guid":{"rendered":"https:\/\/doubleecpu.com\/?page_id=2239"},"modified":"2020-09-19T01:03:41","modified_gmt":"2020-09-19T08:03:41","slug":"packaging-python-projects","status":"publish","type":"page","link":"https:\/\/doubleecpu.com\/index.php\/shop\/python\/packaging-python-projects\/","title":{"rendered":"Packaging Python Projects"},"content":{"rendered":"\n<p>This tutorial demonstrates how to package a simple Python project. It will show you how to create file and folder structure for a python package <br>*how to build the package, and how to upload it to the Python Package Index.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sensor Database Project<\/h2>\n\n\n\n<p>This tutorial will create project named RaspberryPi3B_Sensors_pkg With the following file structure:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RaspberryPi3B_Sensors\n|---SetupFiles\n    |---__init__.py <\/pre>\n\n\n\n<p>The commands must be run from the Top Level Folder:  &#8220;~\/RaspberryPi3B_Sensors\/&#8221; <br>For each Folder the file &#8220;~\/__ init __.py&#8221; is required to import the directory as a package (even if file is empty)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Package Files<\/h2>\n\n\n\n<p>The folders and files that make up the package will be in the Top Level Folder<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RaspberryPi3B_Sensors\n|--- LICENSE\n|--- README.md\n|--  SetupFiles\n     |---__init__.py\n|--  tests\n|--- setup.py<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Creating setup.py<\/h3>\n\n\n\n<p>The build script will contain and run setuptools  module which establishes information about the package created. User name was added to package name to allow forking and customizing  different versions. <\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/gilmedel\/1ae33acd4356725f58d1cf7c1f5df8cc.js\"><\/script><\/p>\n\n\n\n<p>See\u00a0<a href=\"https:\/\/packaging.python.org\/guides\/distributing-packages-using-setuptools\/\">Packaging and distributing projects<\/a>\u00a0for more details on setup().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating README.md<\/h3>\n\n\n\n<p>The read me should include all details about the package, This is loaded into setup().description for this package. The file should describe the package and include as much details as needed to explain package. Example: Contact, URL Links, Overview, Basic Usage, Example descriptions, Change history&#8230; etc.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">A Pyhton Project To automate Sensor Kit use on Raspberry Pi\nVisit <a href=\"https:\/\/doubleecpu.com\">VG-1 Raspberry Pi Blog<\/a>\n<a href=\"https:\/\/doubleecpu.com\"><\/a>\nContact\nGil Medel doubleecpu@outlook.com\nOverview:\nThe Project setsup Raspberry Pi to read sensor data\nSensors are set up and read in python and data is stored to MariaDB (MySQL) and data is Displayed in WordPress website\nBasic Usage:\nExamples:\nWhat's New For Version 0.0.1\n9\/18\/2020\nSetup as Python Package formatting, Set up Test Driven Development Files, Synced To GitHub<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating License for package<\/h2>\n\n\n\n<p>To inform users that install the package the terms under which they can use the package. A great resource for guidance on picking a license <a href=\"https:\/\/choosealicense.com\/\">https:\/\/choosealicense.com\/<\/a>.\u00a0<br>Example: Using an MIT License<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Copyright (c) 2019 Gilbert Medel\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and\/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: \n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Generate Distribution Archives<\/h2>\n\n\n\n<p>To generate distribution packages for a package make sure you have the latest versions of setuptools and wheel installed. This will create archives that are uploaded to the Package Index and can then be installed using pip command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 -m pip install --user --upgrade setuptools wheel<\/pre>\n\n\n\n<p>The command should output text on the command line and once completed should generate two files in the\u00a0<code>dist<\/code>\u00a0directory:<br>dist\/<br>|&#8212; RaspberryPi3B_Sensors_doubleecpu-0.0.1-py3-none-any.whl   <br>|&#8212; RaspberryPi3B_Sensors_doubleecpu-0.0.1.tar.gz<br><br>The\u00a0<code>tar.gz<\/code>\u00a0file is a\u00a0<a href=\"https:\/\/packaging.python.org\/glossary\/#term-Source-Archive\">Source Archive<\/a>\u00a0whereas the\u00a0<code>.whl<\/code>\u00a0file is a\u00a0<a href=\"https:\/\/packaging.python.org\/glossary\/#term-Built-Distribution\">Built Distribution<\/a>. Newer\u00a0<a href=\"https:\/\/packaging.python.org\/key_projects\/#pip\">pip<\/a>\u00a0versions preferentially install built distributions, but will fall back to source archives if needed. It is Required to upload a source archive and provide built archives for the platforms the project is compatible with. For this example package is compatible with Python on any Raspbian Linux so that built distribution is needed along with any other version that should be compatible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Uploading the distribution archives<\/h2>\n\n\n\n<p>   After registering for an account on Test PyPI (a separate instance of the package index for testing before uploading to the real index), URL https:\/\/test.pypi.org\/account\/register\/, complete the steps on that page and verify email address before trying to upload any packages. <br>   Using a PyPI API token allows for securely uploading your package. NEW API Tokens can be created and downloaded  https:\/\/test.pypi.org\/manage\/account\/#api-tokens<br>    *don\u2019t limit its scope to a particular project, since you are creating a new project.<\/p>\n\n\n\n<p><strong>copy and save the token from the page as it will not be available after closing page <\/strong><\/p>\n\n\n\n<p>Install Twine to upload package distribution archives<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 -m pip install --user --upgrade twine<\/pre>\n\n\n\n<p>To upload all of the distribution archives in the dist\/ folder:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 -m twine upload --repository testpypi dist\/*<\/pre>\n\n\n\n<p>When prompted enter username and password.<br>For the username, use <strong>__ token __<\/strong>.<br>For the password, use the token value, including the pypi- prefix.<\/p>\n\n\n\n<p>After the command completes, the output should look similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Uploading distributions to https:\/\/test.pypi.org\/legacy\/ Enter your username: [your username] Enter your password: Uploading RaspberryPi3B_Sensors_doubleecpu-0.0.1-py3-none-any.whl 100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 4.65k\/4.65k [00:01&lt;00:00, 2.88kB\/s] Uploading RaspberryPi3B_Sensors_doubleecpu-0.0.1.tar.gz 100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 4.25k\/4.25k [00:01&lt;00:00, 3.05kB\/s]<\/pre>\n\n\n\n<p>Once uploaded the package should be viewable on TestPyPI, Example,\u00a0https:\/\/test.pypi.org\/project\/RaspberryPi3B_Sensors_doubleecpu<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing package<\/h2>\n\n\n\n<p>\u00a0Packages can be installed using <a href=\"https:\/\/packaging.python.org\/key_projects\/#pip\">pip<\/a>. Create a new\u00a0<a href=\"https:\/\/packaging.python.org\/key_projects\/#virtualenv\">virtualenv<\/a>\u00a0(see\u00a0<a href=\"https:\/\/packaging.python.org\/tutorials\/installing-packages\/\">Installing Packages<\/a>\u00a0for detailed instructions) and install package from TestPyPI:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 -m pip install --index-url https:\/\/test.pypi.org\/simple\/ --no-deps RaspberryPi3B_Sensors_doubleecpu<\/pre>\n\n\n\n<p>*Note This example uses &#8211;index-url flag to specify TestPyPI instead of live PyPI. Additionally, it specifies &#8211;no-deps; pip should install the package from Test PyPI and the output should look something like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Collecting RaspberryPi3B_Sensors_doubleecpu\nDownloading https:\/\/test-files.pythonhosted.org\/packages\/\u2026\/RaspberryPi3B_Sensors_doubleecpu-0.0.1-py3-none-any.whl\nInstalling collected packages: RaspberryPi3B_Sensors_doubleecpu\nSuccessfully installed RaspberryPi3B_Sensors_doubleecpu-0.0.1<\/pre>\n\n\n\n<p>You can test that it was installed correctly by importing the package; either run python venv and >>> import SetupFiles or add to script.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial demonstrates how to package a simple Python project. It will show you how to create file and folder structure for a python package *how to build the package, and how to upload it to the Python Package Index. Sensor Database Project This tutorial will create project named RaspberryPi3B_Sensors_pkg With the following file structure: &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/doubleecpu.com\/index.php\/shop\/python\/packaging-python-projects\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;Packaging Python Projects&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1914,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2239","page","type-page","status-publish","hentry"],"featured_media_urls":[],"_links":{"self":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/2239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/comments?post=2239"}],"version-history":[{"count":0,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/2239\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/1914"}],"wp:attachment":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/media?parent=2239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}