The first pip installation package program experiment
- Often in the use of Python projects and programming, the pip install package name or the easy_install package name is often used for installation of dependent packages.
- By looking at the corresponding guidance documents, it is found that the package can be written and uploaded to the pip official website for others to use.
- URL: https://pypi.org/
Packaging and distributing projects
- First, confirm that the package installation requirements are met, such as the Python version environment, etc.
- Install the "twine" module
pip install twine
- You need to upload your project release to PyPI.
Project configuration that needs to be uploaded
initialization file
setup.py
- The most important file is setup.py , which exists at the root of the project directory. For an example, see setup.py in the PyPA example project .
- setup.py provides two main functions:
- This is the configuration file for various aspects of your project. The main feature of setup.py is that it contains a global setup() function. The keyword arguments to this function are how the specific details of your project are defined. The most relevant arguments are explained in the sections below
- This is the command line interface for running various commands related to packaging tasks. To get a list of available commands, run python setup.py --help-commands.
setup.cfg
- is an ini file that contains default options for the setup.py command. For an example, see setup.cfg in the PyPA example project.
README.rst / README.md files
-
All projects should include a README that covers the project's goals. The most common format is reStructuredText with the "rst" extension, although this is not a requirement; multiple variants of Markdown are also supported (see setup()'s long_description_content_type parameter)
-
For an example, see the README.md in the PyPA example project
注意使用setuptools的项目默认情况下包含在源代码分发中的README.rst(自0.6.27开始)。 内置的distutils库在Python 3.7中开始采用这种行为。 如果您使用setuptools,则不需要在MANIFEST.in中列出README.rst。 否则,包括它是明确的。
MANIFEST.in
LICENSE.txt
your project package
- The package name should preferably be the same as your top-level project package name
- Case: https://github.com/pypa/sampleproject
Precautions
- Python module or package names should obey the following rules:
- all lowercase
- Don't duplicate the package name already on pypi, even if you don't want to publish your package publicly, because your package may act as a dependency of other packages
- Use underscores to separate words or nothing (don't use hyphens)
- Now turn our function into a Python module
start working
- The funniest directory structure is as follows:
pipfunctiontest/ pipfunctiontest/ __init__.py setup.py
The outermost directory is the root directory of our version management tools, such as funniest.git . The subdirectory is also called funniest , which stands for Python module.
- For better understanding, we put the function joke() in init.py:
def joke(): return (u'How do you tell HTML from HTML5?' u'Try it out in Internet Explorer.' u'Does it work?' u'No?' u'It\'s HTML5.')
- The main setup configuration file is setup.py and should contain a line of code that calls setuptools.setup() like this:
from setuptools import setup setup(name='pipfunctiontest', version='v1.0', description='The pipfunctiontest joke in the world', url='http://github.com/storborg/pipfunctiontest', author='Flying Circus', author_email='[email protected]', license = 'MIT', packages=['pipfunctiontest'], zip_safe=False)
- Now we can install this python package locally:
$ python setup.py install
- We can also use the development mode to install this package, without reinstalling every time the code is modified, the latest code is immediately available.:
$ python setup.py develop
- Either way, the package can be used in python after installation:
>>> import pipfunctiontest >>> print pipfunctiontest.joke()
- Published on PyPI
- The script setup.py is also the entry point for registering and uploading source packages on PyPI.
- The first step is to create a source package:
$ python setup.py sdist
- The above register and upload will fail, so you need to use the previously installed twine
twine upload dist/* #Packaged code package
- install this package
- After the above steps are completed, other users can install it directly with easy_install:
easy_install pipfunctiontest
- or use pip
$ pip install pipfunctiontest