When you finish writing your code, do you want to give it to someone else? How do you package your project code? Hey, open source?

Here’s how to package your code.

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

In the todo directory, the files are all grouped together, and we can simply divide them like this:

Here you create a package directory, put the source files in it, and sort out the template files and the database files. In the package, an init.py ritual is also created. To show that you are serious about the project, you can create a tests directory in the project file where you can test your code:

You can then create a LICENSE that says how your project can be used. Is it necessary to declare the source, is it commercially available? How to avoid getting caught and sent to jail when someone else uses your project to screw things up.

There are several open source agreements in place:

Then create a README file that describes what your project does and how to use it.

Now your project file directory looks a bit human:

The next step is to create an important file, setup.py, which describes your project information and allows setupTools to package your project for you. Create a setup.py file in the root directory and write your project information as follows:

What does each parameter mean? Long_description: a long description of the project, which we can read directly from the README file you just wrote.

  • Name: specifies the package name that you define. You can use letters, digits, or underscores (_) to ensure uniqueness.
  • Version: indicates the version number of the project.
  • Author: Your name.
  • Author_email: your email address.
  • Description: A brief description of the project.
  • Long_description_content_type: Type of marker used for long description content, usually markdown or RST.
  • Url: your project’s home page, or link directly to your project’s Github address.
  • Include_package_data: specifies whether to add files other than py.
  • Package_data: List of additional files to add to Python.
  • Packages: Use setupTool directly to find the list of all relevant packages for your project.
  • Use Python3, use MIT protocol, independent of OS
  • Python_requires: Python version requirements. Now, your project catalog should look something like this:

Then you can pack it.

If you haven’t installed setupTools and Wheel before, you can install the following wave:

python3 -m pip install –upgrade setuptools wheel

Then, in the root directory of your project, package it with python3 setup.py sdist bdist_wheel

This is how you add files to your directory:

In the dist directory, the files that end in tar.gz are your project’s source files, and the.whl files are built distributions for others to use.

So how do you get your packaged project out there for others to use? Install twine first: Python3 -m PIP install twine Then use twine to upload your package to pypi :twine upload dist/* Then it will ask you to input the pypi account password:

What if I don’t? To register one, go to the following link :pypi.org/account/reg… Then enter your account password to upload:

At this point someone else can install your project via PIP:

Like this:

Then you can give your bag to someone else:

When you install PIP, you download the WHL file you uploaded and install:

If you don’t want to open source and just want to use it for your friends, you can also send the files in the dist directory to your friends and let them install the wave themselves: