Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a Python package

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to build a Python package", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to build a Python package.

Start

First, you definitely need to set up one or more things to learn how to build the python package. Therefore, the things you need are as follows:

IDE (Vs Code)

Python 3

Build the Python package

The title built in this article is b_dist. B_dist is a distributed package with classes such as Gaussian, Binomial, e.t.c, etc. But first, create a package folder using the following mapping structure:

B_dist/ _ _ init__.py Binomialdistribution.py Guassiandistribution.py Generaldistribution.py licence.txt setup.cfgREADME.mdsetup.py

First, you must create an empty file for those listed above in the mapping. Note: b_dist is a folder and setup.py comes with README files. Md is not in the b_dist folder.

Let's first talk about the files in b_dist:

B_dist/__init__.py

This file tells python that this folder contains a package. In addition, the package is always made up of init files, even if it is empty. When you import a package in the python program, the remaining _ _ init__ file runs. In this case, other _ _ init__ files import Gaussian, binomial, and distribution models so that these classes can be imported directly when using the package.

B_dist/Binomialdistribution.py

Binomial files are classes used to calculate and visualize binomial distribution.

B_dist/Guassiandistribution.py

Gaussian files are classes used to calculate and visualize Gaussian distributions.

B_dist/Generaldistribution.py

General distribution files are classes used to calculate and visualize probability distributions.

B_dist/licence.txt

The license file actually contains your copyright material, indicating that you intend to allow other users to use your package freely.

B_dist/setup.cfg

The cfg file is a file that saves the README file data.

README.md

This is the document of the bag. It describes how the package works

Setup.py

The py file is required for the pip installation package. In addition, it contains metadata about the package. Note the following properties, such as name and package. This property must have the same value as the folder name 'b_dist' to avoid bug when uploading our package.

Run the package locally

Let's first run the package locally by entering the following code:

# change directory to where the setup file and the package is located ~ $cd python_package ~ / python_package:$ # Install the package locally # NOTE:pip install. Installs any setup.py file in that directory. ~ / python_package:$ pip install.

After input, it should be output:

Processing / python_package Building wheels for collected packages: b-dist Building wheel for b-dist (setup.py). Done Created wheel for b-dist: filename=b_dist-0.4-py3-none-any.whl size=5108 sha256=d4c6f74daa1add07f37b01a74294e86ab07d655a6e0944bbb46ed6503ae493ef Stored in directory: / tmp/pip-ephem-wheel-cache-3pvdd9ue/wheels/1e/f9/a3/568195cccd4e2d1dcb1edaf9c2708f651b90b6af6fbdfd3f36 Successfully built b-dist

Finally, our package has been installed. Let's test whether it works by entering the following code:

# open the python shell In [1]: / python_package$ python Out [1] Python 3.7.3 (default, Mar 27 2019, 22:11:17) [GCC 7.3.0]:: Anaconda, Inc. On linux Type "help", "copyright", "credits" or "license" for more information. In [2] > from b_dist import Guassian In [3] > Guassian (10pr 5) Out [3] mean 10, standard deviation 5

Great! now that our bags are ready to work, let's move on to the next part.

Upload Python package

Use PyPI to quickly upload newly built packages. First of all, let's look at what PyPi is. PyPi stands for Python package Index (PyPi), which is the software repository for the Python programming language.

So now we want to upload our package to the test version of the PyPI site, and make sure that the pip installation process is working properly, and then upload it to the PyPI site.

First, create an account with the test. Use the same user name and password for both sites.

After successfully creating two accounts, let's return to IDE and upload the package to TestPyPi. But first, to communicate with these sites, you need pip to install a library called twine, using:

Pip install twine

So, after installing twine, type the following code to upload to TestPyPi first:

# Creating the distribution package to be uploaded ~ / python_package:$ python setup.py sdist

After entering the code, you will see two new folders and move to the next line of code:

# Upload the package created using twinw ~ / python_package:$ twine upload-- repository-url https://test.pypi.org/legacy/ dist/* Output: Uploading distributions to https://test.pypi.org/legacy/ # enter your username and password used in registraion to the site Output: Enter your username: bideen Output: Enter your password:

After entering your password, you will see a successful message: "upload package_name 100% successful". To check whether the upload was successful, please visit your TestPyPi account and view your new package.

Now install pip from the TestPyPi site using the following code:

# first uninstall the previuos package on to aviod conflicts ~ / python_package:$ pip uninstall b_dist # install fro the TestPyPi ~ / python_package:$ pip install-- index-url https://test.pypi.org/simple/ b_dist

After successfully integrating TestPyPi, let's continue uploading to the main PyPi, where you can install pip directly using the package name or publicly use it.

# first uninstall the previuos package on to aviod conflicts ~ / python_package:$ pip uninstall b_dist # install fro the TestPyPi ~ / python_package:$ pip install b_dist here, I believe you have a better understanding of "how to build a Python package". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report