1
0
mirror of https://github.com/balkian/balkian.github.com.git synced 2024-12-25 17:58:12 +00:00
balkian.github.com/post/2014-09-23-publishing-to-pypi/index.html
2019-01-10 19:35:05 +01:00

339 lines
19 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Balkian&#39;s site - WELCOME</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="../../font-awesome/css/all.css">
<link rel="stylesheet" media="only screen" href="../../css/main.css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300|Comfortaa' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="container" class="container">
<div id="contentwrapper">
<div id="content">
<header id="header">
<ul class="navbar" id="navbar">
<a href="../../">
<li >
<i class="fa fa-home fa-large"></i>
</li></a>
<a href="../../post/"><li class=' active' >Blog
</li></a>
<a href="../../tags/"><li class='' >Tags
</li></a>
<a href="../../cheatsheet/"><li class='' >Cheatsheets
</li></a>
<a href="../../about/"><li class='' >About
</li></a>
<a href="../../project/"><li class='' >Projects
</li></a>
<a href="//jfernando.es" target="_blank"><li>
CV
</li></a>
</ul>
</header>
<h2>Publishing on PyPi</h2>
<span class="date">2014/09/27</span>
<span>
<a class="tag" href='../../tags/github'><span class="label">github</span></a>
<a class="tag" href='../../tags/python'><span class="label">python</span></a>
<a class="tag" href='../../tags/pypi'><span class="label">pypi</span></a>
</span>
<h3></h3>
<p>Developing a python module and publishing it on Github is cool, but most
of the times you want others to download and use it easily. That is the
role of PyPi, the python package repository. In this post I show you how
to publish your package in less than 10 minutes.</p>
<h2 id="choose-a-fancy-name">Choose a fancy name</h2>
<p>If you haven&rsquo;t done so yet, take a minute or two to think about this.
To publish on PyPi you need a name for your package that isn&rsquo;t taken.
What&rsquo;s more, a catchy and unique name will help people remember your
module and feel more inclined to at least try it.</p>
<p>The package name should hint what your module does, but that&rsquo;s not
always the case. That&rsquo;s your call. I personally put uniqueness and
memorability over describing the functionality.</p>
<h2 id="create-a-pypirc-configuration-file">Create a .pypirc configuration file</h2>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cfg" data-lang="cfg"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">13
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cfg" data-lang="cfg"><span style="color:#a6e22e">[distutils] # this tells distutils what package indexes you can push to</span>
<span style="color:#a6e22e">index-servers</span> <span style="color:#f92672">=</span><span style="color:#e6db74">
</span><span style="color:#e6db74"> pypi # the live PyPI
</span><span style="color:#e6db74"> pypitest # test PyPI</span>
<span style="color:#a6e22e">[pypi] # authentication details for live PyPI</span>
<span style="color:#a6e22e">repository</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">https://pypi.python.org/pypi</span>
<span style="color:#a6e22e">username</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">{ your_username }</span>
<span style="color:#a6e22e">password</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">{ your_password } # not necessary</span>
<span style="color:#a6e22e">[pypitest] # authentication details for test PyPI</span>
<span style="color:#a6e22e">repository</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">https://testpypi.python.org/pypi</span>
<span style="color:#a6e22e">username</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">{ your_username }</span></code></pre></td></tr></table>
</div>
</div>
<p>As you can see, you need to register both in the <a href="https://pypi.python.org/pypi?%3Aaction=register_form">main pypi
repository</a> and
the <a href="https://testpypi.python.org/pypi?%3Aaction=register_form">testing
server</a>. The
usernames and passwords might be different, that is up to you!</p>
<h2 id="prepare-your-package">Prepare your package</h2>
<p>This should be the structure:</p>
<pre><code>root-dir/ # Any name you want
setup.py
setup.cfg
LICENSE.txt
README.md
mypackage/
__init__.py
foo.py
bar.py
baz.py
</code></pre>
<h3 id="setup-cfg">setup.cfg</h3>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cfg" data-lang="cfg"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cfg" data-lang="cfg"><span style="color:#66d9ef">[metadata]</span>
<span style="color:#a6e22e">description-file</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">README.md</span></code></pre></td></tr></table>
</div>
</div>
<p>The markdown README is the <em>de facto</em> standard in Github, but you can
also use rST (reStructuredText), the standard in the python community.</p>
<h3 id="setup-py">setup.py</h3>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">12
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">from</span> distutils.core <span style="color:#f92672">import</span> setup
setup(name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;mypackage&#39;</span>,
packages <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;mypackage&#39;</span>], <span style="color:#75715e"># this must be the same as the name above</span>
version <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;{ version }&#39;</span>,
description <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;{ description }&#39;</span>,
author <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;{ name }&#39;</span>,
email <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;{ email }&#39;</span>,
url <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;https://github.com/{user}/{package}&#39;</span>, <span style="color:#75715e"># URL to the github repo</span>
download_url <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;https://github.com/{user}/{repo}/tarball/{version}&#39;</span>,
keywords <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;websockets&#39;</span>, <span style="color:#e6db74">&#39;display&#39;</span>, <span style="color:#e6db74">&#39;d3&#39;</span>], <span style="color:#75715e"># list of keywords that represent your package</span>
classifiers <span style="color:#f92672">=</span> [], )</code></pre></td></tr></table>
</div>
</div>
<p>You might notice that the download_url points to a Github URL. We could
host our package anywhere, but Github is a convenient option. To create
the tarball and the zip packages, you only need to tag a tag in your
repository and push it to github:</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">git tag <span style="color:#f92672">{</span>version<span style="color:#f92672">}</span> -m <span style="color:#e6db74">&#34;{ Description of this tag/version}&#34;</span>
git push --tags origin master</code></pre></td></tr></table>
</div>
</div>
<h2 id="push-to-the-testing-main-pypi-server">Push to the testing/main pypi server</h2>
<p>It is advisable that you try your package on the test repository and fix
any problems first. The process is simple:</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">python setup.py register -r <span style="color:#f92672">{</span>pypitest/pypi<span style="color:#f92672">}</span> python setup.py sdist upload -r <span style="color:#f92672">{</span>pypitest/pypi<span style="color:#f92672">}</span></code></pre></td></tr></table>
</div>
</div>
<p>If everything went as expected, you can now install your package through
pip and browse your package&rsquo;s page. For instance, check my senpy
package: <a href="https://pypi.python.org/pypi/senpy">https://pypi.python.org/pypi/senpy</a></p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7c7c79">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">pip install senpy</code></pre></td></tr></table>
</div>
</div>
</div>
<div id="sidebar" class="fixed">
<div id="badge" class="flip-container" ontouchstart="this.classList.toggle('hover');">
<div class="flipper sticky">
<div class="front">
<img id="avatar" src="../../img/me.png">
</div>
<div class="back">
<img id="picture" src="../../img/me-bat.png">
</div>
</div>
</div>
<div id="about">
<div class="icons">
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-linux"></i></span><span class="explanation">Linux user</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-android"></i></span><span class="explanation">Android dev and user</span></span>
<a href="http://github.com/balkian" target="_blank"><span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-github"></i> </span><span class="explanation">Github user</span></span></a>
<a href="http://gitlab.com/balkian" target="_blank"><span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-gitlab"></i> </span><span class="explanation">GitLab user</span></span></a>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-stack-exchange"></i> </span><span class="explanation">StackExchange fan</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-music"></i> </span><span class="explanation">Music lover</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-film"></i> </span><span class="explanation">Movie fan</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-laptop"></i> </span><span class="explanation">Always on a PC</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-moon"></i> </span><span class="explanation">Night owl</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-terminal"></i></span> <span class="explanation">CLI user</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-flag"></i></span> <span class="explanation">I love languages</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-code"></i> </span> <span class="explanation">I love programming</span></span>
</div>
</div>
<div class="entries">
<h2 class="title">Latest entries</h2>
<dt><a href="../../post/2019-01-06-zigbee2mqtt/">Controlling Zigbee devices with MQTT</a></dt>
<a class="tag" href='../../tags/mqtt'><span class="label">mqtt</span></a>
<a class="tag" href='../../tags/iot'><span class="label">iot</span></a>
<a class="tag" href='../../tags/zigbee'><span class="label">zigbee</span></a>
<dt><a href="../../post/2016-09-28-tqdm/">Progress bars in python</a></dt>
<a class="tag" href='../../tags/python'><span class="label">python</span></a>
<dt><a href="../../post/2015-04-10-github-dotfiles/">Sharing dotfiles</a></dt>
<a class="tag" href='../../tags/github'><span class="label">github</span></a>
<a class="tag" href='../../tags/git'><span class="label">git</span></a>
<a class="tag" href='../../tags/dotfiles'><span class="label">dotfiles</span></a>
<dt><a href="../../post/2014-12-09-zotero/">Zotero</a></dt>
<a class="tag" href='../../tags/zotero'><span class="label">zotero</span></a>
<a class="tag" href='../../tags/webdav'><span class="label">webdav</span></a>
<a class="tag" href='../../tags/nginx'><span class="label">nginx</span></a>
<a class="tag" href='../../tags/apache'><span class="label">apache</span></a>
</div>
</div>
<div class="clear"></div>
</div>
<footer class="pagefooter" role="contentinfo">
<div class="contact">
<p>
J. Fernando Sánchez Rada | balkian
</p>
</div>
<ul id="social">
<li><a href="http://github.com/balkian"><i class="fab fa-github"></i></a></li>
<li><a href="http://lab.gsi.upm.es/balkian"><i class="fab fa-gitlab"></i></a></li>
<li><a href="http://twitter.com/balkian"><i class="fab fa-twitter"></i></a></li>
<li><a href="http://git.sinpapel.es/balkian"><i class="fab fa-git"></i></a></li>
<li><a href="http://linkedin.com/in/jfsanchezrada"><i class="fab fa-linkedin"></i></a></li>
</ul>
<p>
<i class="fab fa-creative-commons"></i> Creative Commons A-SA-NC
</p>
</footer>
</div>
<script src="../../js/jquery-2.0.2.min.js"></script>
</body>
</html>