How does python publish its own method steps for pip projects

  • 2020-12-13 19:00:07
  • OfStack

preface

Because I usually write a common logic to a tool python script, like about the time string processing, like about the path and folder traversal what tools. It is too much trouble to copy these tools into each project every time a new project is created and then downloaded from github after a new computer is replaced. Finally, it would be better to build your own pip project.

Environment to prepare

To release the python program with pip, the first step is of course to install Python and pip. Take Ubuntu 16.04 as an example:


$ sudo apt update 
$ sudo apt install -y python python-pip

CentOS and RedHat are installed by default because the RPM system depends on python.

In addition to releasing Pypi, you need to install one of the release tools, twine, and the setuptools and wheel that it depends on:


$ sudo pip install --upgrade twine setuptools wheel

Ok, so we're ready to go here.

Step 1: Register for an PyPi account

Sign up for PyPi

Register 1 own account on the above registration website, register good after activating in mailbox, 1 general registration process is such operation, here is not to say much.

Step 2: Create a project on github

If you have not used github source hosting site, you can first register an github account to create the project, I generally create projects using the official website provided desktop tools Github Desktop.

Github Desktop download address: https: / / desktop github. com

github to register and create new projects, not here, the basic skills of a programmer.

Step 3: Write your own python project

I only have 1 timetool. py and ES67en. py

Details you can check my sample project: https: / / github com/fengmm521 / pipProject

Start by creating a directory, such as magetool, that contains your project code

___ In the magetool directory, create 1 new ___ with ES83en__.py and then the.py document you have to publish

Step 4: Prepare the ES88en.py file

The setup. py file is highlighted here, because the entire pip project was released and uploaded based on this file.

You can see how I wrote setup. py for my example project, which I also downloaded from the Internet.


#!/usr/bin/env python
#-*- coding:utf-8 -*-

#############################################
# File Name: setup.py
# Author: mage
# Mail: mage@woodcol.com
# Created Time: 2018-1-23 19:17:34
#############################################


from setuptools import setup, find_packages

setup(
  name = "magetool",
  version = "0.1.0",
  keywords = ("pip", "pathtool","timetool", "magetool", "mage"),
  description = "time and path tool",
  long_description = "time and path tool",
  license = "MIT Licence",

  url = "https://github.com/fengmm521/pipProject",
  author = "mage",
  author_email = "mage@woodcol.com",

  packages = find_packages(),
  include_package_data = True,
  platforms = "any",
  install_requires = []
)

setup. There are five important parameters in py:

1.name

name = "magetool". This name parameter is the directory where your project code is located and the name of the project that you want to upload to pip. When someone else uses pip install xxx to install, xxx is your magetool

2.version

version = "0.1.0", the version number of your tool. When you update the package with pip, it will update the new version with a higher version than the current version.

Below is the command for pip to update the package


pip install --upgrade < The name of the package >
pip install -U < The name of the package >12

3.packages

packages = find_packages(), this parameter is to import all the packages with each ___ 139EN__ __py under the directory

4.install_requires

install_requires = [], this is an array containing the 3rd library referenced by my pip project. If your project is useful to the 3rd library, add the 3rd library package name here, and if the 3rd library is not the latest version, also have the version number.

Step 5: Pack your own projects

When the code is written and tested by yourself, there are no problems, and you are ready to package.

Packaging uses the following two commands:

I used the second one


$ python setup.py bdist_egg   #  Generate a similar  edssdk-0.0.1-py2.7.egg To support  easy_install 
$ python setup.py sdist     #  Generate a similar  edssdk-0.0.1.tar.gz To support  pip12

When both commands are packaged, build and dist directories are generated, and the packaged files are placed in the dist directory for later uploading to the PyPi server

Step 6: Upload to PyPi server

Before uploading, create a file, $HOME/.pypirc, $HOME under linux or mac is ~/. So I'm going to make a.pypirc file here. It reads as follows:


[distutils]
index-servers = pypi

[pypi]
username: your PyPi The user name 
password: your PyPi password 
~  

Since you cannot upload using python setup. py register, using this upload will result in an error of 410. So use the.pypirc file to save your PyPi user information, and then use the downloaded twine to upload directly.

In other places, it says to upload the project using python setup.py sdist upload command, but in my case, it will still fail 1, so I upload the packaged pip project using the following command. What I uploaded is the packaged project using step 5, python setup.

Upload the packaged pip installation package:


twine upload dist/magetool-0.1.0.tar.gz1

Step 7: Install the package you just uploaded using pip

pip installation package I will not say here, this 1 general use python city

Install the uploaded package with pip:


pip install magetool --user

The user parameter means to install to the current user of the computer. Otherwise, system administrator privileges will be required to install. Add user and you will no longer have sudo administrator privileges.


Related articles: