banner



Python How To Create A Pip Package

Building a Custom PIP Library for Python

a Tutorial in 7 easy steps

T. Pietraßyk

Photo by Iñaki del Olmo on Unsplash

A little research on how to upload packages to pip told me that this should be easy as pie , right? Well I wish it was.

So following Depaak Kumar's tutorial on dzone I jumped right into it and uploaded a couple of functions that I often use.

It took me quite some time and nerves to do it, since the tutorial was a little bit misleading at some points.

So I decided to write my own Tutorial :

1. Install all the needed Package building packages

2. Create new Folders for the necessary files:

This is actually the m ost important step. You need to have the right folder structure to get modules in your Library! Otherwise you cant import anything:

So go to github and create a new repository. Be sure to check initialise with a README and add a license.

On your machine create a parent-folder and navigate to that folder using terminal. Clone your git repository using the following command:

git clone <RepositoryURL>

Now you can create a sub folder that will host your python files. The result should look like this:

Don't worry about the fact that you don't have a setup.py file yet. We will create this right now.

3. Create the setup.py file

in the setup file copy and paste the following text (for more information visit the pypi help section)

4. Fill your Library with code

First of all mage sure, you create a __init__.py file in your Code_goes_here folder. (Just navigate to the folder and run the same lines as you did while creating the setup.py file)

The init file can be left empty or you could provide a nice message as well as some variables describing your Library. Vor example open the file and type in name = "AwsomeLibrary"

Now copy and paste all your python scripts containing the functions that you want to have in your library into the Code_goes_here folder. Make sure they end on .py

5. Setting up pypi

Register at pypi.org. Make sure you validate your e-mail-address before continuing.

Open the terminal and navigate to your home folder by typing in cd and hit Enter. Now create a new file called .pypirc

insert the following code to the .pypirc file and save it

6. Spinning the wheel

Now that you set everything up navigate to your Library_Package folder again (the on that holds the setup.py file) and run the following code:

The first line runs the setup.py script and creates a wheel distribution package that pip will use to install your library.

The second line uploads your library to the pip server.

7. You did it!

You can now use pip install to install your Library on a computer! Great job. To access your functions import your Library using the following syntax:

import <YourLibrary>.<your_py_file_name>

Use your functions as usual and safe some time!

Python How To Create A Pip Package

Source: https://medium.com/@pietrassyk/building-a-custom-pip-library-for-python-fe618034d54a

Posted by: catalanoource1962.blogspot.com

0 Response to "Python How To Create A Pip Package"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel