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 package code and release for Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to package code and release Python". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to package code and release Python"!

Take, for example, our last demonstration of developing a "personal plan todolist" with Python. After writing the code, the directory of the code looks like this:

In the todo directory, the files are all put together, and we can simply divide them as follows:

Here you create a package directory, put the source files in it, and then categorize the template files and database files.

In package, you also created a _ _ init__.py that you often see. What is it? Feel the ritual.

To let others know that you are serious about the project, you can create a tests directory in the project file and test your code in it:

Then you can create a LICENSE to illustrate how your project can be used. Is it necessary to declare the source, and is it commercially available? Other people use your project to make trouble, how to avoid getting caught and going to jail, etc.

Then create a README file to explain what your project is for, how to use descriptions, etc.

Now your project file directory is a bit human:

The next step is to create a more important file-setup.py, which is mainly used to describe your project information so that the setuptools packaging tool can help you package the project.

Create a setup.py file in the root directory and write your project information as follows:

What does each parameter mean? Let me explain this to you:

Long_description: we can read the long description of the project directly from the README file you just wrote.

Name: you can define the package name with letters, numbers, and underscores, and you need to make sure it is unique.

Version: the version number of the project.

Author: your (author) name.

Author_email: your email address.

Description: a brief description of the project.

Long_description_content_type: the type of tag that long describes the use of content, usually markdown or rst.

Url: the home page address of your project, or you can link directly to the Github address of your project.

Include_package_data: whether to add files other than py.

Package_data: a list of additional files that need to be added to Python.

Packages: directly use setuptool to find a list of all relevant packages for your project.

Classifiers: additional instructions, for example, what is written here is for the Python3 version, using the MIT protocol, independent of OS.

Python_requires: python version requirements.

So now, your project directory should look like this:

And then you can pack.

If you haven't installed setuptools and wheel before, you can install it first:

Python3-m pip install-- upgrade setuptools wheel

Then under the root directory of your project, use the following command to package:

Python3 setup.py sdist bdist_wheel

At this point, you will add these files to your directory:

Even if it's packaged, in the dist directory, what ends with tar.gz is the source file of your project, and what ends with .whl is the distribution that has been built for others to use.

So how do you publish your packaged project for others to use?

As a matter of fact, I have said it before, so let's say it briefly here:

Install twine first:

Python3-m pip install twine

Then use twine to upload your packaged file to pypi:

Twine upload dist/*

At this point, it will prompt you to enter the pypi account password:

What if I don't? To sign up for one, register at the following link:

Https://pypi.org/account/register/

Then enter your account password and upload it:

At this point, others can install your project through pip:

Like this:

Then you can use your bag for others:

When you are in pip install, you actually download the whl file you uploaded and install it:

If you don't want to open source, but just want to give it to your friend, you can also send him the files in the dist directory directly and let him install it and use it:

At this point, I believe you have a deeper understanding of "how to package code and release Python". 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