<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chaps</title>
    <description>The latest articles on DEV Community by Chaps (@chaps).</description>
    <link>https://dev.to/chaps</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F253522%2Fa9b2c318-ce94-4a29-b8a4-827e261f2773.jpeg</url>
      <title>DEV Community: Chaps</title>
      <link>https://dev.to/chaps</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chaps"/>
    <language>en</language>
    <item>
      <title>Creating a python package with cookiecutter</title>
      <dc:creator>Chaps</dc:creator>
      <pubDate>Mon, 20 Jan 2020 21:20:07 +0000</pubDate>
      <link>https://dev.to/chaps/creating-a-python-package-with-cookiecutter-3ik0</link>
      <guid>https://dev.to/chaps/creating-a-python-package-with-cookiecutter-3ik0</guid>
      <description>&lt;p&gt;When working with python as your programming language you usually will want to provide your code to others (or to yourself) so end users can make use of it or integrate it as part of a general project, implementation or solution. &lt;/p&gt;

&lt;p&gt;The most common way to achieve this is to provide your code as a python package so that anyone can leverage and use it with a simple import statement &lt;br&gt;
e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;my_python_package&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;by doing this anyone (with access to the package), will not only be able to use it but also to extend it if they need a particular implementation based on your solution. &lt;/p&gt;

&lt;p&gt;An easy way to create a python package is by using the cookiecutter tool, cookiecutter is a python package itself that allows us to create boilerplates dynamically, avoiding having to copy/clone the base source and editing it, cookiecutter templates can be written in a way that they end up requesting input from the user in order to set different variables used across the template.&lt;/p&gt;

&lt;p&gt;By following this guide we will create an example python package&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a virtual environment with your desired interpreter:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; venv virtcookiecutter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;This will leave a directory named “virtcookiecutter” under the CurrentWorkingDirectory where the previous command was executed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Activate your virtual environment:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source &lt;/span&gt;virtcookiecutter/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I got into an issue where while running the next step, the pip installation process raised a not recognized command which comes from a missing dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;error: invalid &lt;span class="nb"&gt;command&lt;/span&gt; &lt;span class="s1"&gt;'bdist_wheel'&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we will install the dependency (wheel) for the bdist_wheel command to exist:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;wheel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Next, we will need to install cookiecutter in our (now activated) virtual environment
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;cookiecutter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;And let’s use cookiecutter with the cookiecutter template for a python package as it's argument:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cookiecutter template creation will prompt some questions in order to set some variables used across the process:&lt;/p&gt;

&lt;p&gt;You can basically search where are any of the requested variables being used in the template source code by getting the template sources &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;e.g.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/audreyr/cookiecutter-pypackage.git 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 ‑ &lt;br&gt;
 ) and recursively searching for the variable name (they will be referenced across the template like this &lt;/p&gt;

&lt;p&gt;e.g. *cookiecutter.variable_name * &lt;/p&gt;

&lt;p&gt;Example prompt and inputs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;full_name &lt;span class="o"&gt;[&lt;/span&gt;Audrey Roy Greenfeld]: Ernesto Solis Diaz
email &lt;span class="o"&gt;[&lt;/span&gt;audreyr@example.com]: example@domain.org
github_username &lt;span class="o"&gt;[&lt;/span&gt;audreyr]: chaps
project_name &lt;span class="o"&gt;[&lt;/span&gt;Python Boilerplate]: Python Cookiecutter Example
project_slug &lt;span class="o"&gt;[&lt;/span&gt;python_cookiecutter_example]:      
project_short_description &lt;span class="o"&gt;[&lt;/span&gt;Python Boilerplate contains all the boilerplate you need to create a Python package.]: An initial example of using cookiecutter
pypi_username &lt;span class="o"&gt;[&lt;/span&gt;chaps]: 
version &lt;span class="o"&gt;[&lt;/span&gt;0.1.0]: 
use_pytest &lt;span class="o"&gt;[&lt;/span&gt;n]: 
use_pypi_deployment_with_travis &lt;span class="o"&gt;[&lt;/span&gt;y]: 
add_pyup_badge &lt;span class="o"&gt;[&lt;/span&gt;n]: 
Select command_line_interface:
1 - Click
2 - Argparse
3 - No command-line interface
Choose from 1, 2, 3 &lt;span class="o"&gt;[&lt;/span&gt;1]: 2
create_author_file &lt;span class="o"&gt;[&lt;/span&gt;y]: 
Select open_source_license:
1 - MIT license
2 - BSD license
3 - ISC license
4 - Apache Software License 2.0
5 - GNU General Public License v3
6 - Not open &lt;span class="nb"&gt;source
&lt;/span&gt;Choose from 1, 2, 3, 4, 5, 6 &lt;span class="o"&gt;[&lt;/span&gt;1]: 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will end up creating a directory with the name of the project_slug, inside this directory, you’ll find the contents of the boilerplate with the values you used during the prompt process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls &lt;/span&gt;python_cookiecutter_example/
AUTHORS.rst  CONTRIBUTING.rst  docs  HISTORY.rst  LICENSE  Makefile  MANIFEST.in  python_cookiecutter_example  README.rst  requirements_dev.txt  setup.cfg  setup.py  tests  tox.ini
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can install your created python package:&lt;/p&gt;

&lt;p&gt;Change your current working directory into the created python package directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;python_cookiecutter_example/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And install your package with pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Processing /path/to/python_cookiecutter_example
Installing collected packages: python-cookiecutter-example
  Running setup.py install for python-cookiecutter-example ... done
Successfully installed python-cookiecutter-example-0.1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;List your current packages and see python-cookiecutter-example being installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; pip freeze
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output: (notice python-cookiecutter-example==0.1.0 )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arrow==0.15.5
binaryornot==0.4.4
certifi==2019.11.28
chardet==3.0.4
Click==7.0
cookiecutter==1.7.0
future==0.18.2
idna==2.8
Jinja2==2.10.3
jinja2-time==0.2.0
MarkupSafe==1.1.1
pkg-resources==0.0.0
poyo==0.5.0
python-cookiecutter-example==0.1.0
python-dateutil==2.8.1
requests==2.22.0
six==1.13.0
urllib3==1.25.7
whichcraft==0.6.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open a python REPL and examine some of the package created information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

\&amp;gt;&amp;gt;&amp;gt; import python_cookiecutter_example
\&amp;gt;&amp;gt;&amp;gt; dir(python_cookiecutter_example)
['__author__', '__builtins__', '__cached__', '__doc__', '__email__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__']
\&amp;gt;&amp;gt;&amp;gt; python_cookiecutter_example.__author__
'Ernesto Solis Diaz'
You can add your package sources under the project_slug directory in order to add contents to your package and be able to import them. 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cookiecutter.readthedocs.io/"&gt;https://cookiecutter.readthedocs.io/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cookiecutter.readthedocs.io/en/1.7.0/installation.html"&gt;https://cookiecutter.readthedocs.io/en/1.7.0/installation.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cookiecutter.readthedocs.io/en/1.7.0/tutorial1.html#step-1-generate-a-python-package-project"&gt;https://cookiecutter.readthedocs.io/en/1.7.0/tutorial1.html#step-1-generate-a-python-package-project&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to leave any comment on feedback :)  &lt;/p&gt;

</description>
      <category>python</category>
      <category>cookiecutter</category>
    </item>
  </channel>
</rss>
