{"id":170,"date":"2021-10-06T09:45:06","date_gmt":"2021-10-06T09:45:06","guid":{"rendered":"http:\/\/ouyangminwei.com\/?p=170"},"modified":"2021-10-06T09:49:19","modified_gmt":"2021-10-06T09:49:19","slug":"how-to-develop-you-python-package","status":"publish","type":"post","link":"https:\/\/ouyangminwei.com\/index.php\/2021\/10\/06\/how-to-develop-you-python-package\/","title":{"rendered":"How to develop your python package ?"},"content":{"rendered":"\n<p>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 &#8220;pip&#8221;, 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.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-1024x467.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-1024x467.png\"\/><\/figure>\n\n\n\n<p>There are few step for you to develop your own package.<\/p>\n\n\n\n<ol class=\"has-normal-font-size wp-block-list\"><li>Prepare your code.<\/li><li>Create a PyPi account.<\/li><li>Prepare the files PyPi needs.<\/li><li>upload it to PyPi<\/li><li>pip install your package.<\/li><li>Upload it to github<\/li><\/ol>\n\n\n\n<h1 class=\"has-text-color wp-block-heading\" style=\"color:#00aaff\">Code<\/h1>\n\n\n\n<p>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).<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/OuYangMinOa\/ou_Axion_limit\">https:\/\/github.com\/OuYangMinOa\/ou_Axion_limit<\/a><\/p>\n\n\n\n<p>This code have two class in two different File<\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:27% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"116\" height=\"90\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-1.png\" alt=\"\" class=\"wp-image-173 size-full\"\/><\/figure><div class=\"wp-block-media-text__content\">\n<ul class=\"has-medium-font-size wp-block-list\"><li>class Glimit in GLimit.py<\/li><li>class analyse in Analy.py<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<h1 class=\"has-text-color wp-block-heading\" style=\"color:#00aaff\">PyPi account<\/h1>\n\n\n\n<p>Create a PyPi account for us to upload our package,<\/p>\n\n\n\n<p><a href=\"https:\/\/pypi.org\/account\/register\/\">https:\/\/pypi.org\/account\/register\/<\/a><\/p>\n\n\n\n<h1 class=\"has-text-color wp-block-heading\" style=\"color:#00aaff\"> Prepare the files PyPi needs. <\/h1>\n\n\n\n<p>Now, I wish my classes are all in a same package call ou_Axion_limit<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># my expectation\nfrom ou_Axion_limit import Glimit\nfrom ou_Axion_limit import analyse <\/code><\/pre>\n\n\n\n<p> 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.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-3.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-3.png\"\/><\/figure>\n\n\n\n<h2 class=\"has-text-color wp-block-heading\" style=\"color:#f131ed\">__init__.py<\/h2>\n\n\n\n<p>Creata a file __init__.py inside the  ou_Axion_limit<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/OuYangMinOa\/f77422cd1579a8ffe70d6a5370b8ea78.js\"><\/script>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># my expectation\nfrom ou_Axion_limit import Glimit\nfrom ou_Axion_limit import analyse<\/code><\/pre>\n\n\n\n<h2 class=\"has-text-color wp-block-heading\" style=\"color:#f131ed\">setup.py<\/h2>\n\n\n\n<p>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:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/OuYangMinOa\/1b2e9376e5284f61d73118c01d5577c0.js\"><\/script>\n\n\n\n<h2 class=\"has-text-color wp-block-heading\" style=\"color:#f131ed\">CHANGELOG.txt<\/h2>\n\n\n\n<p>The changelog of you package<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/OuYangMinOa\/a56b4b77bf2cdaffdbaabbacdccbae7c.js\"><\/script>\n\n\n\n<h2 class=\"has-text-color wp-block-heading\" style=\"color:#f131ed\">LICENSE.txt<\/h2>\n\n\n\n<p>Use this file to define all license details, you can use your own license, however, I will use MIT license<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/OuYangMinOa\/db9999886946d94d9754fc02adecae2f.js\"><\/script>\n\n\n\n<h2 class=\"has-text-color wp-block-heading\" style=\"color:#f131ed\">MANIFEST.in<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/OuYangMinOa\/4dd9f5245831aad17f3db279b852c199.js\"><\/script>\n\n\n\n<p>Just a file use to include everything<\/p>\n\n\n\n<h2 class=\"has-text-color wp-block-heading\" style=\"color:#f131ed\">README.md<\/h2>\n\n\n\n<p>A markdown file can make people understand your package better.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/OuYangMinOa\/3885f099d0a03154e9e6412a8b87ef43.js\"><\/script>\n\n\n\n<h1 class=\"has-text-color wp-block-heading\" style=\"color:#00aaff\"> Upload it to PyPi <\/h1>\n\n\n\n<p>Everything is prepared, we can now upload it to PyPi.<\/p>\n\n\n\n<p>First, goto you folder open cmd and tpye:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python setup.py sdist<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-4-1024x613.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-4-1024x613.png\"\/><\/figure>\n\n\n\n<p>If you see UserWarning: Unknown distribution option: &#8216;install_requires&#8217;, just ignore it.<\/p>\n\n\n\n<p>It should create a file in dist folder<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-6.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-6.png\"\/><\/figure>\n\n\n\n<p>Next, we need twine for upload the package<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install twine<\/code><\/pre>\n\n\n\n<p>Then, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>twine upload dist\/{Generated_file}<\/code><\/pre>\n\n\n\n<p>You will be asked to provide your username and password. Provide your credentials you used  to register with PyPi.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-7-1024x160.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-7-1024x160.png\"\/><\/figure>\n\n\n\n<p><strong>Congratulations<\/strong>, you successfully upload your packge to the PyPi, goto the website it gives you to see your package online.<\/p>\n\n\n\n<h1 class=\"has-text-color wp-block-heading\" style=\"color:#00aaff\">Install your package<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install YOURPACKAGENAME<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-8-1024x487.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-8-1024x487.png\"\/><\/figure>\n\n\n\n<h1 class=\"has-text-color wp-block-heading\" style=\"color:#00aaff\">Upload to github<\/h1>\n\n\n\n<p>For the fist time using git, we need to create a ssh-key to connect to github<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-10-1024x223.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-10-1024x223.png\"\/><figcaption>I create a ssh key in   C:\\Users\\USER\/.ssh\/<\/figcaption><\/figure>\n\n\n\n<p>It did exist.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-11.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-11.png\"\/><\/figure>\n\n\n\n<p>Show the content<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type filename<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-12.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-12.png\"\/><\/figure>\n\n\n\n<p>Copy everything and add in to your account.<\/p>\n\n\n\n<p><a href=\"https:\/\/docs.github.com\/en\/authentication\/connecting-to-github-with-ssh\/adding-a-new-ssh-key-to-your-github-account\">https:\/\/docs.github.com\/en\/authentication\/connecting-to-github-with-ssh\/adding-a-new-ssh-key-to-your-github-account<\/a><\/p>\n\n\n\n<p>Create your repository<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"232\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-14.png\" alt=\"\" class=\"wp-image-187\" srcset=\"https:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-14.png 314w, https:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-14-300x222.png 300w, https:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-14-150x111.png 150w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"591\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-13-1024x591.png\" alt=\"\" class=\"wp-image-186\" srcset=\"https:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-13-1024x591.png 1024w, https:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-13-300x173.png 300w, https:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-13-150x87.png 150w, https:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-13-768x443.png 768w, https:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-13.png 1093w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Then upload your project.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git init\ngit add .\ngit commit -n \"first upload\"\ngit remote add origin git@github:{name\/project}\ngit push -u origin master<\/code><\/pre>\n\n\n\n<p>If you want to upload new code later, you only need to type the following commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git add .\ngit commit -m \"v1.0.0\"\ngit push<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/10\/image-15.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-15.png\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>pip is a software package management sys &hellip; <a href=\"https:\/\/ouyangminwei.com\/index.php\/2021\/10\/06\/how-to-develop-you-python-package\/\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[7,3],"post_format":[],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-en","tag-python"],"_edit_lock":"1633513760:1","_edit_last":"1","_aioseo_title":"#post_title #separator_sa #site_title","_aioseo_description":"#post_excerpt","_aioseo_keywords":"","_aioseo_og_title":"","_aioseo_og_description":"","_aioseo_og_article_section":"","_aioseo_og_article_tags":"","_aioseo_twitter_title":"","_aioseo_twitter_description":"","_oembed_2544c1d0cb3503ab4c4d558c3b3c8873":"","_oembed_time_2544c1d0cb3503ab4c4d558c3b3c8873":"","_oembed_99481806ecbe6ce4ee46f8588d320993":"","_oembed_db663acf973e82e6d9d80df71945dfb8":"","_oembed_16cdfab488f57db73586f4286af2704f":"","_wp_old_slug":"","_links":{"self":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":7,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/tags?post=170"},{"taxonomy":"post_format","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/post_format?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}