How to develop your python package ?

pip is a software package management system written in the Python computer programming language. It can install and manage software packages, with a simple command “pip”, you can get access to more then one hundred thousand of cool libraries, here I am going to show you how to upload your own code to PyPi and able to download with pip.

這張圖片的 alt 屬性值為空,它的檔案名稱為 image-1024x467.png

There are few step for you to develop your own package.

  1. Prepare your code.
  2. Create a PyPi account.
  3. Prepare the files PyPi needs.
  4. upload it to PyPi
  5. pip install your package.
  6. Upload it to github

Code

In here, I want to upload the class I wrote for research in the institute, If you are interested, It is a code for calculate the limit of TASEH(Taiwan Axion Search Experiment Haloscope).

https://github.com/OuYangMinOa/ou_Axion_limit

This code have two class in two different File

  • class Glimit in GLimit.py
  • class analyse in Analy.py

PyPi account

Create a PyPi account for us to upload our package,

https://pypi.org/account/register/

Prepare the files PyPi needs.

Now, I wish my classes are all in a same package call ou_Axion_limit

# my expectation
from ou_Axion_limit import Glimit
from ou_Axion_limit import analyse 

so first create a folder (Glimit), this folder will content every thing PyPi needs, and next create a floder name with your package name (ou_Axion_limit ), inside the folder you just created, and throw you python code inside.

這張圖片的 alt 屬性值為空,它的檔案名稱為 image-3.png

__init__.py

Creata a file __init__.py inside the ou_Axion_limit

This file allows you to determine the classes that users can directly call and use, so in my cases, I can just import my class with following way.

# my expectation
from ou_Axion_limit import Glimit
from ou_Axion_limit import analyse

setup.py

The setup.py file contains the information that PyPi needed , such as its name, description, current version, etc. Copy and paste the following code and replace the string with the matching content:

CHANGELOG.txt

The changelog of you package

LICENSE.txt

Use this file to define all license details, you can use your own license, however, I will use MIT license

MANIFEST.in

Just a file use to include everything

README.md

A markdown file can make people understand your package better.

Upload it to PyPi

Everything is prepared, we can now upload it to PyPi.

First, goto you folder open cmd and tpye:

python setup.py sdist
這張圖片的 alt 屬性值為空,它的檔案名稱為 image-4-1024x613.png

If you see UserWarning: Unknown distribution option: ‘install_requires’, just ignore it.

It should create a file in dist folder

這張圖片的 alt 屬性值為空,它的檔案名稱為 image-6.png

Next, we need twine for upload the package

pip install twine

Then, run the following command:

twine upload dist/{Generated_file}

You will be asked to provide your username and password. Provide your credentials you used to register with PyPi.

這張圖片的 alt 屬性值為空,它的檔案名稱為 image-7-1024x160.png

Congratulations, you successfully upload your packge to the PyPi, goto the website it gives you to see your package online.

Install your package

pip install YOURPACKAGENAME
這張圖片的 alt 屬性值為空,它的檔案名稱為 image-8-1024x487.png

Upload to github

For the fist time using git, we need to create a ssh-key to connect to github

ssh-keygen
這張圖片的 alt 屬性值為空,它的檔案名稱為 image-10-1024x223.png
I create a ssh key in C:\Users\USER/.ssh/

It did exist.

這張圖片的 alt 屬性值為空,它的檔案名稱為 image-11.png

Show the content

type filename
這張圖片的 alt 屬性值為空,它的檔案名稱為 image-12.png

Copy everything and add in to your account.

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

Create your repository

Then upload your project.

git init
git add .
git commit -n "first upload"
git remote add origin git@github:{name/project}
git push -u origin master

If you want to upload new code later, you only need to type the following commands.

git add .
git commit -m "v1.0.0"
git push
這張圖片的 alt 屬性值為空,它的檔案名稱為 image-15.png
分類: Uncategorized,標籤: , 。這篇內容的永久連結

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *