avatarAlain Saamego

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3238

Abstract

er?utm_source=medium&utm_medium=referral">Christopher Gower</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p id="028d"><b>Packaging Your Library</b></p><p id="96dd">Once you’ve written the code for your library, the next step is to package it up for distribution.</p><p id="64f0">There are a few different ways to package up Python libraries, but we’ll be using the setuptools package in this tutorial. Setuptools makes it easy to package Python libraries for distribution, and also provides a number of features that we’ll make use of later on.</p><p id="a1a0">To install setuptools, simply run the following command:</p><div id="7ec4"><pre>pip <span class="hljs-keyword">install</span> setuptools</pre></div><p id="e6c2">Once setuptools has been installed, you can create a file called setup.py in the same directory as your mylib.py file. This file will contain the configuration information for your library.</p><p id="0d08">The setup.py file for our mylib library might look something like this:</p><div id="1ba7"><pre><span class="hljs-keyword">from</span> setuptools <span class="hljs-keyword">import</span> setup</pre></div><div id="d53b"><pre><span class="hljs-selector-tag">setup</span>( name=’mylib’, version=’<span class="hljs-number">1.0</span>.<span class="hljs-number">0</span>', description=’A sample Python library’, author=’Your Name’, author_email=’your<span class="hljs-variable">@email</span>.com’, url=’<span class="hljs-attribute">https</span>:<span class="hljs-comment">//github.com/yourname/mylib',</span> py_modules=[‘mylib’], )</pre></div><p id="bb0d">Let’s take a look at each of the options in the setup.py file:</p><p id="e107"><b>name —</b> This is the name of your library. It should be all lowercase and can contain underscores.</p><p id="4ad3"><b>version — </b>This is the version number of your library. It should follow the format major.minor.patch, where major, minor, and patch are all integers.</p><p id="255c"><b>description —</b> This is a short description of your library.</p><p id="9229"><b>author — </b>This is the name of the author of your library.</p><p id="9f81"><b>author_email — </b>This is the email address of the author of your library.</p><p id="5086"><b>url — </b>This is the URL of your library’s homepage.</p><p id="3f4a"><b>py_modules — </b>This is a list of modules that your library contains. In this case, we’re just including the mylib module that we wrote earlier.</p><p id="1433">With our setup.py file in place, we can now package up our library for distribution. To do so, simply run the following command:</p><div id="4775"><pre><span class="hljs-keyword">python</span> setup.<span class="hljs-keyword">py</span> sdist</pre></div><p id="2cce">This will create a file called mylib-1.0.0.tar.gz in the dist/ directory. This file contains our packaged Python library.</p><figure id="c984"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*75tVnmyzbkwC3RR9"><figcaption>Photo by <a href="https://unsplash.com/@ilyapavlov?utm_source=medium&amp;utm_medium=referral">Ilya Pavlov</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaptio

Options

n></figure><p id="2a51"><b>Registering Your Library</b></p><p id="6b78"><b>Now that we have our library packaged up and ready for distribution, the next step is to register it with the Python Package Index (PyPI). Registering our library will make it available for others to install via the pip package manager.</b></p><p id="42b8">To register our library, we’ll need to create an account on the PyPI website. Once you’ve created an account and logged in, you should be taken to the account management page. From here, you can click on the “Edit Profile” link to edit your account information.</p><p id="4ae4">One of the fields on the profile editing page is called “OpenID URL”. This is the URL that will be used to identify you when you upload your library to PyPI.</p><p id="94e4">For the purposes of this tutorial, we’ll assume that your OpenID URL is <a href="https://example.com/">https://example.com/</a>.</p><p id="9417"><b>Uploading Your Library</b></p><p id="43d9">With our library registered with PyPI, we can now upload it for others to install.</p><p id="4594">To upload your library, simply run the following command:</p><div id="0632"><pre>python setup.py <span class="hljs-keyword">register</span> -r pypi</pre></div><p id="831d">This will upload the mylib-1.0.0.tar.gz file that we created earlier to the PyPI server. Once the upload is complete, your library will be available for others to install!</p><p id="4796"><b>Installing Your Library</b></p><p id="3aa3">Now that your library is uploaded to PyPI, others can install it by running the following command:</p><div id="1a0f"><pre>pip <span class="hljs-keyword">install</span> mylib</pre></div><p id="6316">This will download and install your library, and all of its dependencies, into their Python environment.</p><p id="6b8a">In this tutorial, we’ve walked through the process of publishing your own Python library, step by step.</p><p id="cca8">We’ve covered everything from writing the code for your library to packaging it up for distribution to uploading it to the Python Package Index (PyPI).</p><p id="fc8a">By following the steps in this tutorial, you’ll be ready to share your own Python libraries with the world in no time!</p><p id="5418"><b><i>Before you leave:</i></b></p><p id="9d16">If you liked this article, don’t forget to give me a few claps, f<a href="https://medium.com/@alains">ollow me </a>and thus receive all updates about new publications.</p><p id="79a0">If you enjoy reading stories like these, consider <a href="https://medium.com/@alains/membership">signing up</a> to become a <a href="https://medium.com/@alains/membership">Medium member</a>. It’s $5 a month, and you’ll receive unlimited access to stories on Medium.</p><p id="d8fd">So don’t wait — <a href="https://medium.com/@alains/membership">sign up now</a> and start enjoying all that Medium has to offer.</p><p id="55d1"><b>About the author: <i>Alain Saamego</i></b>: Software engineer, writer and content strategist at <a href="https://selfgrow.co.uk">SelfGrow.co.uk</a></p><p id="9557"><b><i>Email</i></b>:[email protected]</p><p id="88bd"><a href="https://twitter.com/alainsamego"><i>Follow me on Twitter </i></a><i>if you want even more content.</i></p></article></body>

Python Tutorial: How to Publish Your Own Python Library Step By Step

By the end of this tutorial, you’ll be ready to publish your own Python library!.

Photo by Hitesh Choudhary on Unsplash

As a Python developer, sooner or later you’ll want to create and publish your own Python library. Doing so can be a great way to share your code with the world, and can also be a helpful way to make money as a developer.

In this tutorial, we’ll walk through the process of publishing your own Python library, step by step. We’ll cover everything from writing the code for your library to packaging it up for distribution to uploading it to the Python Package Index (PyPI).

By the end of this tutorial, you’ll be ready to publish your own Python library!

Writing the Code for Your Library

Before we can publish our Python library, we first need to write the code for it.

For the purposes of this tutorial, we’ll assume that you’ve already written the code for your library and that it is stored in a file called mylib.py.

If you haven’t written the code for your library yet, don’t worry! There are many resources available to help you learn how to code in Python.

Once you’ve written the code for your library, you can continue with this tutorial.

Photo by Christopher Gower on Unsplash

Packaging Your Library

Once you’ve written the code for your library, the next step is to package it up for distribution.

There are a few different ways to package up Python libraries, but we’ll be using the setuptools package in this tutorial. Setuptools makes it easy to package Python libraries for distribution, and also provides a number of features that we’ll make use of later on.

To install setuptools, simply run the following command:

pip install setuptools

Once setuptools has been installed, you can create a file called setup.py in the same directory as your mylib.py file. This file will contain the configuration information for your library.

The setup.py file for our mylib library might look something like this:

from setuptools import setup
setup(
 name=’mylib’,
 version=’1.0.0',
 description=’A sample Python library’,
 author=’Your Name’,
 author_email=’your@email.com’,
 url=’https://github.com/yourname/mylib',
 py_modules=[‘mylib’],
)

Let’s take a look at each of the options in the setup.py file:

name — This is the name of your library. It should be all lowercase and can contain underscores.

version — This is the version number of your library. It should follow the format major.minor.patch, where major, minor, and patch are all integers.

description — This is a short description of your library.

author — This is the name of the author of your library.

author_email — This is the email address of the author of your library.

url — This is the URL of your library’s homepage.

py_modules — This is a list of modules that your library contains. In this case, we’re just including the mylib module that we wrote earlier.

With our setup.py file in place, we can now package up our library for distribution. To do so, simply run the following command:

python setup.py sdist

This will create a file called mylib-1.0.0.tar.gz in the dist/ directory. This file contains our packaged Python library.

Photo by Ilya Pavlov on Unsplash

Registering Your Library

Now that we have our library packaged up and ready for distribution, the next step is to register it with the Python Package Index (PyPI). Registering our library will make it available for others to install via the pip package manager.

To register our library, we’ll need to create an account on the PyPI website. Once you’ve created an account and logged in, you should be taken to the account management page. From here, you can click on the “Edit Profile” link to edit your account information.

One of the fields on the profile editing page is called “OpenID URL”. This is the URL that will be used to identify you when you upload your library to PyPI.

For the purposes of this tutorial, we’ll assume that your OpenID URL is https://example.com/.

Uploading Your Library

With our library registered with PyPI, we can now upload it for others to install.

To upload your library, simply run the following command:

python setup.py register -r pypi

This will upload the mylib-1.0.0.tar.gz file that we created earlier to the PyPI server. Once the upload is complete, your library will be available for others to install!

Installing Your Library

Now that your library is uploaded to PyPI, others can install it by running the following command:

pip install mylib

This will download and install your library, and all of its dependencies, into their Python environment.

In this tutorial, we’ve walked through the process of publishing your own Python library, step by step.

We’ve covered everything from writing the code for your library to packaging it up for distribution to uploading it to the Python Package Index (PyPI).

By following the steps in this tutorial, you’ll be ready to share your own Python libraries with the world in no time!

Before you leave:

If you liked this article, don’t forget to give me a few claps, follow me and thus receive all updates about new publications.

If you enjoy reading stories like these, consider signing up to become a Medium member. It’s $5 a month, and you’ll receive unlimited access to stories on Medium.

So don’t wait — sign up now and start enjoying all that Medium has to offer.

About the author: Alain Saamego: Software engineer, writer and content strategist at SelfGrow.co.uk

Email:[email protected]

Follow me on Twitter if you want even more content.

Python
Python3
Programming
Data Science
Machine Learning
Recommended from ReadMedium