How to create a python 2.x package - simple case

前端 未结 2 1276
萌比男神i
萌比男神i 2020-12-12 17:01

Please show the simple and up to date standard way to create a python package for python 2.x

I\'d prefer to use pip for installing the package later.

The pac

相关标签:
2条回答
  • 2020-12-12 17:13

    Start simple

    Simplest one-file package:

    MyProject/
        setup.py
        my_package.py
    

    Simplest setup.py:

    from setuptools import setup
    setup(name='MyProject',
          version='0.1',
          author='Your Name',
          author_email='your.name@example.com',
          license='MIT',
          description='Example package that says hello',
          py_modules=['my_package'])
    

    Including extra files in the package

    Next you should probably add a README:

    MyProject/
        MANIFEST.in
        README.rst
        setup.py
        my_package.py
    

    Note the new file -- MANIFEST.in. It specifies which non-Python files ought to be included in your source distribution:

    include *.rst
    

    People will tell you "oh, skip the manifest, just add the files to source control, setuptools will find them". Ignore that advice, it's too error-prone.

    Making the PyPI page useful

    It's useful to make the README.rst available for people to view online, on the Python Package Index. So change your setup.py to do

    from setuptools import setup
    with open('README.rst') as f:
        readme = f.read()
    setup(name='MyProject',
          ...
          description='Example package that says hello',
          long_description=readme,
          ...)
    

    Use ReStructuredText markup for prettier pages. Use

    python setup.py --long-description | rst2html
    

    to catch ReStructuredText errors early.

    More than one Python module in a package

    One file will not be enough soon, so change it to a package (confusing terminology warning: Python package as in a directory with a __init__ py, not as in a distributable self-contained archive):

    MyProject/
        MANIFEST.in
        README.rst
        setup.py
        my_package/
            __init__.py
            some_module.py
    

    and change setup.py to

    from setuptools import setup, find_packages
    with open('README.rst') as f:
        readme = f.read()
    setup(name='MyProject',
          version='0.2',
          author='Your Name',
          author_email='your@email',
          license='MIT',
          description='Example package that says hello',
          long_description=readme,
          packages=find_packages())
    

    Releasing to the public

    Get a PyPI account -- you only need to do this once.

    To make a release, make sure the version number in setup.py is correct, then run

    python setup.py sdist register upload
    

    That's it.

    Telling people to install it

    Tell them to

    pip install MyProject
    

    (same name you specified in setup.py as the name argument to setup())

    0 讨论(0)
  • 2020-12-12 17:16

    The following is copied from the Distutils Tutorial.

    File layout:

    top
    |-- package
    |   |-- __init__.py
    |   |-- module.py
    |   `-- things
    |       |-- cross.png
    |       |-- fplogo.png
    |       `-- tick.png
    |-- runner
    |-- MANIFEST.in
    |-- README
    `-- setup.py
    

    To make the installation tarball, you simply run:

    python setup.py sdist
    

    To install the package, use pip or easy_install:

    pip install my_package-1.2.3.tar.bz2
    

    or

    easy_install my_package-1.2.3.tar.bz2
    

    Also, you can upload it to PyPI, first register it:

    python setup.py register
    

    then upload the source tarball

    python setup.py sdist upload
    

    You can upload binary eggs as well (though not necessary):

    python setup.py bdist_egg upload
    

    Then folks can install it like this:

    pip install my_package==1.2.3
    

    or,

    easy_install my_package==1.2.3
    
    0 讨论(0)
提交回复
热议问题