After the Python project code is written, how to package and release it?

After the Python project code is written, how to package and release it?

Xiaoshuai b The correct posture for learning python

After you have finished writing your code, do you want to use it for others? How to package your project code?

Hello, is it open source?

After the Python project code is written, how to package and release it?

Next, Xiaoshuai b will tell you how to package your code.

Take the todo we demonstrated last time as an example. After writing the code, the code directory looks like this:

After the Python project code is written, how to package and release it?

In the todo directory, the files are all put together. We can simply divide it like this:

After the Python project code is written, how to package and release it?

Create a package directory here, put the source code files into it, and then arrange the template files and database files into separate categories.

In the package, it is also creating a init sense of what .py ceremony.

In order to let others know that you are taking this project seriously, you can create a tests directory in the project file and test your code in it:

After the Python project code is written, how to package and release it?

Then you can create a LICENSE to illustrate how your project can be used? Do I need to declare the source? Is it commercially available? Others use your project to do things, how to avoid being caught and going to jail, etc.

There are several kinds of open source agreements, if you don't know much, you can check this one I wrote before: What do those open source agreements declared by GitHub mean?

After the Python project code is written, how to package and release it?

Then create a README file to explain what your project does, how to use it, and other descriptions.

After the Python project code is written, how to package and release it?

Now your project file directory is a bit human-looking:

After the Python project code is written, how to package and release it?

Next, we need to create a more important file-setup.py, this file 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 in the following way:

After the Python project code is written, how to package and release it?

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

long_description: For a long description of the project, we can read it directly from the README file you just wrote.

After the Python project code is written, how to package and release it?

name: The package name you define, you can use letters, numbers, and underscores to ensure uniqueness.

version: The version number of the project.

author: Your (author) name.

author_email: Your (author) email address.

description: A brief description of the project.

long_description_content_type: The mark type used for long description content, generally markdown or rst.

url: The homepage address of your project, or you can directly link 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: Use setuptool directly to find a list of all relevant packages for your project.

classifiers: Additional explanation, for example, what is written here is for Python3 version, using MIT protocol, independent of OS.

python_requires: python version requirements.

So now, your project directory should look like this:

After the Python project code is written, how to package and release it?

Then it can be packaged.

If you have not installed setuptools and wheel before, you can install a wave first:

python3 -m pip install --upgrade setuptools wheel

After the Python project code is written, how to package and release it?

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

python3 setup.py sdist bdist_wheel

After the Python project code is written, how to package and release it?

At this time, your directory will add such files:

After the Python project code is written, how to package and release it?

In this way, even if it is packaged, in the dist directory, the source files ending in tar.gz are the source files of your project, and the distribution ending in .whl is the build for others to use.

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

In fact, I have said it before, let's briefly talk about it here:

Install twine first:

python3 -m pip install twine

Then use twine to upload your packaged files to pypi:

twine upload dist/*

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

After the Python project code is written, how to package and release it?

What if there is no? To register for one, go to the following link to register:
https://pypi.org/account/register/

Then enter your account password, you can upload it:

After the Python project code is written, how to package and release it?

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

After the Python project code is written, how to package and release it?
After the Python project code is written, how to package and release it?

like this:

After the Python project code is written, how to package and release it?

Then you can use your package for others:

After the Python project code is written, how to package and release it?

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

After the Python project code is written, how to package and release it?

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

After the Python project code is written, how to package and release it?
After the Python project code is written, how to package and release it?

Ok, the above is the sharing brought to you by Xiaoshuai b today, I hope it will be helpful to you, then we will see you next time, peace!

Guess you like

Origin blog.51cto.com/15082392/2644470