如何创建python 2.x包-简单案例 [英] How to create a python 2.x package - simple case

查看:167
本文介绍了如何创建python 2.x包-简单案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请说明为python 2.x创建python包的简单且最新的标准方法.

我希望以后使用pip来安装软件包.

该程序包应包含一个类:

class hello:
  def greet(self):
    print "hello"

一个人以后应该可以执行以下操作:

pip install my_package-0.1.1....

然后使用它:

from my_package import hello

h = hello.hello()
h.greet()

我要的是:

  • 目录和文件布局
  • 文件内容
  • 创建可分发软件包文件的命令
  • 从可分发软件包文件中安装软件包的命令(最好使用pip)

我找到了几种方法,但是我仍然不确定如何处理这种非常简单而精简的案例(没有嵌套包,删除所有文件和在最简单的情况下可以省略的功能),以及该如何处理.是现代的方法.

我希望这个问题进入社区Wiki状态,因此您将不会获得任何积分,我将给出足够的时间,并在几天后标记一个接受的答案,同时还要考虑投票和评论.

我有一个要分享的第一个示例,我使用了Marius Gedminas的答案.它没有包含应该存在的所有内容,但是可以正常工作,因此可以演示技术流程的核心.要添加更多必要的部分,请阅读以下Marius的答案.

目录结构:

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

setup.py:

from setuptools.import setup
setup(name='MyProject',
      version='0.1',
      py_modules=['my_package'])

my_package.py:

class hello:
  def greet(self):
    print "hello"

MANIFEST.in:

include *.txt

要从此文件夹创建软件包,请进入文件夹MyProject并运行:

$ python setup.py sdist

这将在子文件夹dist/中创建文件MyProject-0.1.tar.gz.将此文件复制到目标计算机上的文件夹中.

在目标计算机上,在包含MyProject-0.1.tar.gz的文件夹中运行以下命令:

sudo pip install MyProject-0.1.tar.gz

现在可能需要注销并重新登录目标计算机,这样才能找到该软件包.然后,您可以使用python shell在目标计算机上测试软件包:

$ python
>>> import my_package
>>> h = my_package.hello()
>>> h.greet()
hello
>>> 

一旦成功,请记住添加其他必要的内容,请参阅下面的马里乌斯回答.

解决方案

从简单开始

最简单的一文件包:

MyProject/
    setup.py
    my_package.py

最简单的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'])

在包中包括多余的文件

接下来,您可能应该添加自述文件:

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

注意新文件- MANIFEST.in .它指定哪些非Python文件应包含在您的源代码发行版中:

include *.rst

人们会告诉您哦,跳过清单,只需将文件添加到源代码管理中,setuptools会找到它们".忽略该建议,它太容易出错.

使PyPI页面有用

在README.rst上使人们可以在Python Package Index上在线查看非常有用.因此,将您的setup.py更改为

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,
      ...)

对漂亮的页面使用 ReStructuredText 标记.使用

python setup.py --long-description | rst2html

尽早发现ReStructuredText错误.

一个包中有多个Python模块

一个文件很快就不够用,所以将其更改为一个包(令人困惑的术语警告:Python包位于具有__init__ py的目录中,而不是可分发的自包含存档中):

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

并将setup.py更改为

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())

向公众发布

获取PyPI帐户-您只需执行一次. /p>

要发布版本,请确保setup.py中的版本号正确,然后运行

python setup.py sdist register upload

就是这样.

告诉人们安装它

告诉他们

pip install MyProject

(您在setup.py中指定的名称与setup()的name参数相同)

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 package should contain a single class:

class hello:
  def greet(self):
    print "hello"

One should be able to do the following later:

pip install my_package-0.1.1....

And then using it:

from my_package import hello

h = hello.hello()
h.greet()

What I am asking for is:

  • The directory and file layout
  • Contents of the files
  • command to create the distributable package file
  • command to install the package from the distributable package file (using preferably pip)

There are several howtos that I found but I am still not sure how this very simple and stripped down case (no nested packages, removal off all files and features that can be omitted for the most simple case) would be handled and which is the modern way to do it.

I would like this question to enter community wiki state, so you won't get any points and I will give enough time and will mark an answer accepted after several days, also considering the votes and comments.

Edit:

I have a first running example that I want to share, I used Marius Gedminas's answer for it. It does not contain everything that should be there, but it works, so it can demonstrate the core of the technical process. To add more necessary parts please read Marius's answer below.

Directory structure:

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

setup.py:

from setuptools.import setup
setup(name='MyProject',
      version='0.1',
      py_modules=['my_package'])

my_package.py:

class hello:
  def greet(self):
    print "hello"

MANIFEST.in:

include *.txt

To create the package from this folder, go into the folder MyProject and run:

$ python setup.py sdist

This will create a file MyProject-0.1.tar.gz in a subfolder dist/. Copy this file to a folder on the target machine.

On the target machine run this command in the folder containing MyProject-0.1.tar.gz:

sudo pip install MyProject-0.1.tar.gz

It can be necessary to logout and re-login on the target machine now, so the package will be found. Afterwards you can test the package on the target machine using the python shell:

$ python
>>> import my_package
>>> h = my_package.hello()
>>> h.greet()
hello
>>> 

Once this works please remember to add the other necessary contents, see Marius's answer below.

解决方案

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())

这篇关于如何创建python 2.x包-简单案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆