这个“setup.py”有什么问题?

发布于 2024-08-08 18:27:52 字数 1254 浏览 4 评论 0原文

我在让 setup.py 正确执行 sdist 方面遇到了问题。我把它归结为这一点。我有以下目录结构:

my_package\
    my_subpackage\
        __init__.py
        deep_module.py
    __init__.py
    module.py
    setup.py

这是我在 setup.py 中的内容:(

#!/usr/bin/env python

from distutils.core import setup
import distutils

setup(
    name='a',
    version='0.1',
    description='a',
    author='a',
    author_email='[email protected]',
    url='http://a.org',
    packages=['my_package','my_package.my_subpackage'],
    package_dir={'': '..'},
    license= "a",
    long_description = 'aaa',

)

'aaa' 只是占位符。)

无论如何,当我这样做时它工作正常setup.py install,但是当我尝试执行 setup.py sdist 时,发生了一些奇怪的事情:

  1. A MANIFEST 文件是创建。

  2. 在现有 my_package 文件夹内部创建 my_package 文件夹的副本(尽管它丢失了一些我设置的相关文件) )

  3. 创建了一个 dist 文件夹,里面有一个 zip 文件,里面有一个带有包名称的文件夹,但在该文件夹内没有像我这样的整个包希望但只有两个文件,setup.pyPKG-INFO

我做错了什么?如何使 sdist 工作?

I've been having problems withe getting setup.py to do the sdist thing correctly. I boiled it down to this. I have the following directory structure:

my_package\
    my_subpackage\
        __init__.py
        deep_module.py
    __init__.py
    module.py
    setup.py

And here's what I have in setup.py:

#!/usr/bin/env python

from distutils.core import setup
import distutils

setup(
    name='a',
    version='0.1',
    description='a',
    author='a',
    author_email='[email protected]',
    url='http://a.org',
    packages=['my_package','my_package.my_subpackage'],
    package_dir={'': '..'},
    license= "a",
    long_description = 'aaa',

)

(The 'aaa' stuff is just placeholder.)

Anyway, it works okay when I do setup.py install, but when I try to do setup.py sdist, a few curious things happen:

  1. A MANIFEST file is created.

  2. A copy of the my_package folder is created inside the existing my_package folder (though it misses a few of the setup-related files I think.)

  3. A dist folder is created, inside it a zipfile, inside that a folder with the package name, but inside that folder there isn't the whole package like I hoped but only two files, setup.py and PKG-INFO.

What am I doing wrong? How do I make sdist work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女情怀诗 2024-08-15 18:27:52

这个问题在这里得到了很好的解释:

Setuptools 有许多无声故障
模式。其中之一是未能
包含 sdist 版本中的所有文件
(嗯,不完全是失败,你可以
RTFM,但默认行为是
意外)。这篇文章将作为
谷歌自己回答这个问题
问题,直到我们变得新的、更闪亮的,
分发解决我们所有的问题
问题。

正如评论所指出的,这个错误(设计错误)实际上是在 distutils 中——setuptools 只是无法修复它(如果你使用 svn,情况实际上会好一点)。

我可以在您观察时重现您的问题,即稍微缩短文件名,我有:

$ ls -lR
total 8
-rw-r--r--  1 aleax  eng    0 Oct 24 11:25 __init__.py
-rw-r--r--  1 aleax  eng    0 Oct 24 11:25 modu.py
drwxr-xr-x  4 aleax  eng  136 Oct 24 11:25 mysub
-rw-r--r--  1 aleax  eng  323 Oct 24 11:26 setup.py

./mysub:
total 0
-rw-r--r--  1 aleax  eng  0 Oct 24 11:25 __init__.py
-rw-r--r--  1 aleax  eng  0 Oct 24 11:25 deepmod.py

并且运行 python setup.py sdist 会产生(以及警告):

$ ls -lR
total 16
-rw-r--r--  1 aleax  eng  104 Oct 24 11:35 MANIFEST
-rw-r--r--  2 aleax  eng    0 Oct 24 11:25 __init__.py
drwxr-xr-x  3 aleax  eng  102 Oct 24 11:35 dist
-rw-r--r--  2 aleax  eng    0 Oct 24 11:25 modu.py
drwxr-xr-x  5 aleax  eng  170 Oct 24 11:35 mypack
drwxr-xr-x  4 aleax  eng  136 Oct 24 11:25 mysub
-rw-r--r--  1 aleax  eng  323 Oct 24 11:26 setup.py

./dist:
total 8
-rw-r--r--  1 aleax  eng  483 Oct 24 11:35 a-0.1.tar.gz

./mypack:
total 0
-rw-r--r--  2 aleax  eng    0 Oct 24 11:25 __init__.py
-rw-r--r--  2 aleax  eng    0 Oct 24 11:25 modu.py
drwxr-xr-x  4 aleax  eng  136 Oct 24 11:35 mysub

./mypack/mysub:
total 0
-rw-r--r--  2 aleax  eng  0 Oct 24 11:25 __init__.py
-rw-r--r--  2 aleax  eng  0 Oct 24 11:25 deepmod.py

./mysub:
total 0
-rw-r--r--  2 aleax  eng  0 Oct 24 11:25 __init__.py
-rw-r--r--  2 aleax  eng  0 Oct 24 11:25 deepmod.py

一种解决方案是更改目录布局如下(来自当前的 mypack 目录):

$ mkdir mypack
$ mv __init__.py modu.py mysub/ mypack
$ touch README.txt

所以得到:(

$ ls -lR
total 8
-rw-r--r--  1 aleax  eng    0 Oct 24 11:37 README.txt
drwxr-xr-x  5 aleax  eng  170 Oct 24 11:37 mypack
-rw-r--r--  1 aleax  eng  323 Oct 24 11:26 setup.py

./mypack:
total 0
-rw-r--r--  1 aleax  eng    0 Oct 24 11:25 __init__.py
-rw-r--r--  1 aleax  eng    0 Oct 24 11:25 modu.py
drwxr-xr-x  4 aleax  eng  136 Oct 24 11:25 mysub

./mypack/mysub:
total 0
-rw-r--r--  1 aleax  eng  0 Oct 24 11:25 __init__.py
-rw-r--r--  1 aleax  eng  0 Oct 24 11:25 deepmod.py

并删除其中一个警告,即关于 README 的警告——关于丢失 MANIFEST.in 的警告显然仍然存在;-)。还将 setup.py 的一行更改为:

package_dir={'': '.'},

现在,在 python setup.py sdist 之后,您确实得到了一个不错的 tarball:

$ tar tvf dist/a-0.1.tar.gz 
drwxr-xr-x aleax/eng         0 2009-10-24 11:40:05 a-0.1/
drwxr-xr-x aleax/eng         0 2009-10-24 11:40:05 a-0.1/mypack/
-rw-r--r-- aleax/eng         0 2009-10-24 11:25:30 a-0.1/mypack/__init__.py
-rw-r--r-- aleax/eng         0 2009-10-24 11:25:30 a-0.1/mypack/modu.py
drwxr-xr-x aleax/eng         0 2009-10-24 11:40:05 a-0.1/mypack/mysub/
-rw-r--r-- aleax/eng         0 2009-10-24 11:25:30 a-0.1/mypack/mysub/__init__.py
-rw-r--r-- aleax/eng         0 2009-10-24 11:25:30 a-0.1/mypack/mysub/deepmod.py
-rw-r--r-- aleax/eng       156 2009-10-24 11:40:05 a-0.1/PKG-INFO
-rw-r--r-- aleax/eng         0 2009-10-24 11:37:41 a-0.1/README.txt
-rw-r--r-- aleax/eng       322 2009-10-24 11:39:46 a-0.1/setup.py

当然,MANIFEST 文件仍然在您当前的目录中创建,但我希望这是不是问题。

The problem is well explained here:

Setuptools has many silent failure
modes. One of them is failure to
include all files in sdist release
(well not exactly a failure, you could
RTFM, but the default behavior is
unexpected). This post will serve as a
google-yourself-answer for this
problem, until we get new, shinier,
Distribute solving all of our
problems.

As comments point out, the bug (misdesign) is actually in distutils -- setuptools just fails to fix it (if you're using svn, things are actually a bit better).

I can reproduce your problem as you observe it, i.e., shortening file names a bit, I have:

$ ls -lR
total 8
-rw-r--r--  1 aleax  eng    0 Oct 24 11:25 __init__.py
-rw-r--r--  1 aleax  eng    0 Oct 24 11:25 modu.py
drwxr-xr-x  4 aleax  eng  136 Oct 24 11:25 mysub
-rw-r--r--  1 aleax  eng  323 Oct 24 11:26 setup.py

./mysub:
total 0
-rw-r--r--  1 aleax  eng  0 Oct 24 11:25 __init__.py
-rw-r--r--  1 aleax  eng  0 Oct 24 11:25 deepmod.py

and running python setup.py sdist produces (as well as warnings):

$ ls -lR
total 16
-rw-r--r--  1 aleax  eng  104 Oct 24 11:35 MANIFEST
-rw-r--r--  2 aleax  eng    0 Oct 24 11:25 __init__.py
drwxr-xr-x  3 aleax  eng  102 Oct 24 11:35 dist
-rw-r--r--  2 aleax  eng    0 Oct 24 11:25 modu.py
drwxr-xr-x  5 aleax  eng  170 Oct 24 11:35 mypack
drwxr-xr-x  4 aleax  eng  136 Oct 24 11:25 mysub
-rw-r--r--  1 aleax  eng  323 Oct 24 11:26 setup.py

./dist:
total 8
-rw-r--r--  1 aleax  eng  483 Oct 24 11:35 a-0.1.tar.gz

./mypack:
total 0
-rw-r--r--  2 aleax  eng    0 Oct 24 11:25 __init__.py
-rw-r--r--  2 aleax  eng    0 Oct 24 11:25 modu.py
drwxr-xr-x  4 aleax  eng  136 Oct 24 11:35 mysub

./mypack/mysub:
total 0
-rw-r--r--  2 aleax  eng  0 Oct 24 11:25 __init__.py
-rw-r--r--  2 aleax  eng  0 Oct 24 11:25 deepmod.py

./mysub:
total 0
-rw-r--r--  2 aleax  eng  0 Oct 24 11:25 __init__.py
-rw-r--r--  2 aleax  eng  0 Oct 24 11:25 deepmod.py

One solution is to change the directory layout as follows (from the current mypack dir):

$ mkdir mypack
$ mv __init__.py modu.py mysub/ mypack
$ touch README.txt

so getting:

$ ls -lR
total 8
-rw-r--r--  1 aleax  eng    0 Oct 24 11:37 README.txt
drwxr-xr-x  5 aleax  eng  170 Oct 24 11:37 mypack
-rw-r--r--  1 aleax  eng  323 Oct 24 11:26 setup.py

./mypack:
total 0
-rw-r--r--  1 aleax  eng    0 Oct 24 11:25 __init__.py
-rw-r--r--  1 aleax  eng    0 Oct 24 11:25 modu.py
drwxr-xr-x  4 aleax  eng  136 Oct 24 11:25 mysub

./mypack/mysub:
total 0
-rw-r--r--  1 aleax  eng  0 Oct 24 11:25 __init__.py
-rw-r--r--  1 aleax  eng  0 Oct 24 11:25 deepmod.py

(and getting rid of one of the warnings, the one about README -- the one about missing MANIFEST.in clearly remains;-). Also change one line of setup.py to:

package_dir={'': '.'},

Now, after python setup.py sdist, you do get a decent tarball:

$ tar tvf dist/a-0.1.tar.gz 
drwxr-xr-x aleax/eng         0 2009-10-24 11:40:05 a-0.1/
drwxr-xr-x aleax/eng         0 2009-10-24 11:40:05 a-0.1/mypack/
-rw-r--r-- aleax/eng         0 2009-10-24 11:25:30 a-0.1/mypack/__init__.py
-rw-r--r-- aleax/eng         0 2009-10-24 11:25:30 a-0.1/mypack/modu.py
drwxr-xr-x aleax/eng         0 2009-10-24 11:40:05 a-0.1/mypack/mysub/
-rw-r--r-- aleax/eng         0 2009-10-24 11:25:30 a-0.1/mypack/mysub/__init__.py
-rw-r--r-- aleax/eng         0 2009-10-24 11:25:30 a-0.1/mypack/mysub/deepmod.py
-rw-r--r-- aleax/eng       156 2009-10-24 11:40:05 a-0.1/PKG-INFO
-rw-r--r-- aleax/eng         0 2009-10-24 11:37:41 a-0.1/README.txt
-rw-r--r-- aleax/eng       322 2009-10-24 11:39:46 a-0.1/setup.py

the MANIFEST file is still created in your current directory of course, but I hope that's not a problem.

葵雨 2024-08-15 18:27:52

而不是这个:

my_package\
    my_subpackage\
        __init__.py
        deep_module.py
    __init__.py
    module.py
    setup.py

尝试这个:

my_package_source\
    setup.py
    README.txt
    my_package\
        my_subpackage\
            __init__.py
            deep_module.py
        __init__.py
        module.py

您实际上不需要自述文件,它只是为了说明项目文件夹的根目录中包含哪些内容。

===编辑========================================

我应该详细说明。运行它后,您的目录应如下所示:

my_package_source\
    setup.py
    README.txt
    MANIFEST
    PKG-INFO
    dist\
        my_package_0.X.tar.gz (or .zip on windows I believe)
    my_package\
        my_subpackage\
            __init__.py
            deep_module.py
        __init__.py
        module.py

使用 dist 目录下的包进行分发。

Instead of this:

my_package\
    my_subpackage\
        __init__.py
        deep_module.py
    __init__.py
    module.py
    setup.py

Try this:

my_package_source\
    setup.py
    README.txt
    my_package\
        my_subpackage\
            __init__.py
            deep_module.py
        __init__.py
        module.py

You don't actually need a README, it's just for illustrative purpose for what kind of things sit in the root directory of your project's folder.

=== EDIT ======================================

I should elaborate. After you run it your directory should then look something like this:

my_package_source\
    setup.py
    README.txt
    MANIFEST
    PKG-INFO
    dist\
        my_package_0.X.tar.gz (or .zip on windows I believe)
    my_package\
        my_subpackage\
            __init__.py
            deep_module.py
        __init__.py
        module.py

Use the package under the dist directory to distribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文