The way to bundle a Python app (pip) for PyPi


On this tutorial, we’ll create a Python software that may be put in immediately from pip that can present the ten newest weblog posts from [this website][1] (the one you’re studying this on!).

In case you simply wish to keep away from all of this difficult work and publish a python script immediately, you’ll be able to at all times make use of my makepip bundle proper from the command line.

Study extra about Makepip right here.

Getting began

Be sure to have registered at Pypy and have an account, we’ll want this to add our bundle as soon as we’re performed.

Now create a listing to work from:

mkdir -p ~/src/tmp/aogl_feed && cd $_

In our new listing, make certain to have a [python virtual environment][2] to make life a bit of easier.

virtualenv -p python3 venv

And activate it:

supply venv/bin/activate

Now guarantee that we’ve all of the neccessary issues put in to finish this tutorial efficiently.

python -m pip set up --upgrade pip setuptools wheel
python -m pip set up tqdm
python -m pip set up twine

Creating our construction and information

At this stage we must always create our listing construction for our bundle:

As a result of our bundle will likely be fairly easy to reveal what it takes, create the next 4 information:

LICENCE
README.md
aogl/
    __init__.py
    __main__.py
    aogo.py
setup.py

Within the licence file, you’ll be able to place the next (customise it as you want):

Copyright (c) 2020 Andrew O 

Permission is hereby granted, freed from cost, to any particular person acquiring a duplicate
of this software program and related documentation information (the "Software program"), to deal
within the Software program with out restriction, together with with out limitation the rights
to make use of, copy, modify, merge, publish, distribute, sublicense, and/or promote
copies of the Software program, and to allow individuals to whom the Software program is
furnished to take action, topic to the next circumstances:

The above copyright discover and this permission discover shall be included in all
copies or substantial parts of the Software program.

THE 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 OTHER
LIABILITY, 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.

For the readme file, we’ll add this:

A python bundle to retrieve the most recent 10 weblog posts from 

Within the setup.py file, we configure all our project-specific info:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.learn()

setuptools.setup(
    identify='aogl',  
    model='0.1',
    creator="Andrew Odendaal",
    author_email="[email protected]",
    description="A python bundle to retrieve the most recent 10 weblog posts from ",
    long_description=long_description,
    long_description_content_type="textual content/markdown",
    url="
    packages=["aogl"],
    entry_points = {
        "console_scripts": ['aogl = aogl.aogl:main']
    },
    install_requires=[
        "requests",
        "feedparser"
    ],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)

Create a repository to retailer every thing

It’s possible you’ll discover that we’ve listed the url key to level to https://github.com/ao/aogl_pip, which doesn’t but exist, so let’s go and create that.

Go to Github and create a brand new repository.

We’ll identify ours aogl_pip to match with what we advised setup.py it could be.

We don’t wish to initialise with a README, as we have already got one. Click on Create repository to proceed.

We are able to now push to our new repository, however we aren’t fairly prepared but. So let’s end up our native setup.

Add our major code

At this level, we are able to create our aoms.py file talked about above and populate it with the next code:

#!/usr/bin/env python

import feedparser, requests
response = requests.get("/feed")
feed = feedparser.parse(response.content material)

feed.entries = feed.entries[0:9]

for entry in feed.entries:
  print(entry.title)
  print(entry.hyperlinks[0].href)
  print()

Additionally, make certain to set it to be executable:

chmod +x aogl.py

Allow us to additionally create the clean aogl/__init__.py file and the aogl/__main__.py file which comprises the next code:

from .aogl import major
major()

As we’ve used a pair dependencies ourselves, will ought to set up them to our digital atmosphere as follows:

pip set up requests
pip set up feedparser

Distributing these dependencies is simple once we permit individuals to put in our app by way of pip, as a result of we’ve specified these dependencies within the setup.py file, keep in mind this block?

install_requires=[
     "requests",
     "feedparser"
],

Now once we run python aogl.py, our script prints out the ten newest weblog posts for us.

Construct our bundle

It’s now time to construct our bundle and push it to Pypi.

We do that by working:

python setup.py bdist_wheel

This creates an entire lot of information for us, the output appears to be like one thing like this:

working sdist
working egg_info
creating aogl.egg-info
writing aogl.egg-info/PKG-INFO
writing dependency_links to aogl.egg-info/dependency_links.txt
writing entry factors to aogl.egg-info/entry_points.txt
writing top-level names to aogl.egg-info/top_level.txt
writing manifest file 'aogl.egg-info/SOURCES.txt'
studying manifest file 'aogl.egg-info/SOURCES.txt'
writing manifest file 'aogl.egg-info/SOURCES.txt'
working verify
creating aogl-0.1
creating aogl-0.1/aogl
creating aogl-0.1/aogl.egg-info
copying information to aogl-0.1...
copying README.md -> aogl-0.1
copying setup.py -> aogl-0.1
copying aogl/__init__.py -> aogl-0.1/aogl
copying aogl/__main__.py -> aogl-0.1/aogl
copying aogl/aogl.py -> aogl-0.1/aogl
copying aogl.egg-info/PKG-INFO -> aogl-0.1/aogl.egg-info
copying aogl.egg-info/SOURCES.txt -> aogl-0.1/aogl.egg-info
copying aogl.egg-info/dependency_links.txt -> aogl-0.1/aogl.egg-info
copying aogl.egg-info/entry_points.txt -> aogl-0.1/aogl.egg-info
copying aogl.egg-info/top_level.txt -> aogl-0.1/aogl.egg-info
Writing aogl-0.1/setup.cfg
creating dist
Creating tar archive
eradicating 'aogl-0.1' (and every thing underneath it)
working bdist_wheel
working construct
working build_py
creating construct
creating construct/lib
creating construct/lib/aogl
copying aogl/__init__.py -> construct/lib/aogl
copying aogl/aogl.py -> construct/lib/aogl
copying aogl/__main__.py -> construct/lib/aogl
putting in to construct/bdist.macosx-10.15-x86_64/wheel
working set up
working install_lib
creating construct/bdist.macosx-10.15-x86_64
creating construct/bdist.macosx-10.15-x86_64/wheel
creating construct/bdist.macosx-10.15-x86_64/wheel/aogl
copying construct/lib/aogl/__init__.py -> construct/bdist.macosx-10.15-x86_64/wheel/aogl
copying construct/lib/aogl/aogl.py -> construct/bdist.macosx-10.15-x86_64/wheel/aogl
copying construct/lib/aogl/__main__.py -> construct/bdist.macosx-10.15-x86_64/wheel/aogl
working install_egg_info
Copying aogl.egg-info to construct/bdist.macosx-10.15-x86_64/wheel/aogl-0.1-py3.7.egg-info
working install_scripts
including license file "LICENCE" (matched sample "LICEN[CS]E*")
creating construct/bdist.macosx-10.15-x86_64/wheel/aogl-0.1.dist-info/WHEEL
creating 'dist/aogl-0.1-py3-none-any.whl' and including 'construct/bdist.macosx-10.15-x86_64/wheel' to it
including 'aogl/__init__.py'
including 'aogl/__main__.py'
including 'aogl/aogl.py'
including 'aogl-0.1.dist-info/LICENCE'
including 'aogl-0.1.dist-info/METADATA'
including 'aogl-0.1.dist-info/WHEEL'
including 'aogl-0.1.dist-info/entry_points.txt'
including 'aogl-0.1.dist-info/top_level.txt'
including 'aogl-0.1.dist-info/RECORD'
eradicating construct/bdist.macosx-10.15-x86_64/wheel

Take a look at our bundle

Let’s check out what was created; utilizing the tree command, we restrict the output to 2 ranges of depth:

tree -L 2

.
├── LICENCE
├── README.md
├── aogl
│   ├── __init__.py
│   ├── __main__.py
│   └── aogl.py
├── aogl.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   ├── entry_points.txt
│   └── top_level.txt
├── construct
│   ├── bdist.macosx-10.15-x86_64
│   └── lib
├── dist
│   ├── aogl-0.1-py3-none-any.whl
│   └── aogl-0.1.tar.gz
├── setup.py
└── venv
    ├── bin
    ├── embrace
    └── lib

We are able to see there’s a construct listing, which comprises construct bundle info.

The aogl.egg.information listing comprises all dependency and bundle info.

There’s additionally a dist listing, which comprises our *.whl, which is a Wheel file.

This wheel could be put in listing with pip if we wished to, by working pip set up dist/aogl-0.1-py3-none-any.whl.

We’ll really do that to guarantee that every thing works as anticipated earlier than we publish our new code to the world.

It really works fairly nicely!

Let’s uninstall this native pip, in order that we’re capable of set up it from Pypi as soon as it’s efficiently pushed.

pip uninstall aogl

Add our code to Pypi

Subsequent we’ll create a file underneath our house listing referred to as ~/.pypirc and configure it:

[distutils] 
index-servers=pypi

[pypi] 
repository =  
username = aogl

My username occurs to be the identical because the bundle I’m at the moment constructing, so make certain to regulate the username worth to no matter your registered username is on the Pypi web site.

Now we are able to use twine to add our wheel.

python -m twine add dist/*

If all was profitable, we must always see this:

Importing distributions to 
Enter your password:
Importing aogl-0.1-py3-none-any.whl
100%|█████████████████████████████████████████████| 5.89k/5.89k [00:00<00:00, 7.29kB/s]
NOTE: Strive --verbose to see response content material.
HTTPError: 403 Shopper Error: Invalid or non-existent authentication info. See  for particulars for url: 
(venv) ➜  aogl_feed python -m twine add dist/*
Importing distributions to 
Enter your password:
Importing aogl-0.1-py3-none-any.whl
100%|█████████████████████████████████████████████| 5.89k/5.89k [00:03<00:00, 1.96kB/s]
Importing aogl-0.1.tar.gz
100%|█████████████████████████████████████████████| 4.37k/4.37k [00:01<00:00, 2.93kB/s]

View at:
https:&#47;&#47;pypi.org/venture/aogl/0.1/

Our bundle is now out there at https://pypi.org/venture/aogl/0.1/

Push our code to Github

Don’t overlook to push the code to Github, in order that we are able to replace new variations later.

git init
git add LICENCE README.md aogl/ setup.py
git commit -m 'Pushing code for aogl model 0.1'
git distant add origin 
git push -u origin grasp
```<determine class="wp-block-image size-large">

<img decoding="async" loading="lazy" width="1202" peak="722" src=" alt="" class="wp-image-3922" srcset="/wp-content/uploads/2020/02/image-20.png 1202w, /wp-content/uploads/2020/02/image-20-300x180.png 300w, /wp-content/uploads/2020/02/image-20-1024x615.png 1024w, /wp-content/uploads/2020/02/image-20-768x461.png 768w" sizes="(max-width: 1202px) 100vw, 1202px" /> </determine> 

## Take a look at every thing because the world would see it

Lastly we get to check out putting in our new bundle utilizing pip off of Pypi!

pip set up aogl


It put in efficiently!

aogl


<img decoding="async" loading="lazy" width="1132" peak="932" src=" alt="" class="wp-image-3925" srcset="/wp-content/uploads/2020/02/image-22.png 1132w, /wp-content/uploads/2020/02/image-22-300x247.png 300w, /wp-content/uploads/2020/02/image-22-1024x843.png 1024w, /wp-content/uploads/2020/02/image-22-768x632.png 768w" sizes="(max-width: 1132px) 100vw, 1132px" /> </determine> 

Our fantastic new contribution to the world has returned an inventory of the most recent 10 weblog posts!

Wonderful! Job performed.

Keep in mind, you may as well use the `<a rel="noreferrer noopener" aria-label="makepip (opens in a brand new tab)" href=" goal="_blank">makepip</a>` bundle to do all of this mechanically for you!

[Learn more about Makepip here][3].

 [1]: 
 [2]: /how-to-setup-and-use-the-python-virtual-environment/
 [3]: /makepip/

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles