在编写 setup.py 文件时如何包含修改后的第 3 方模块?
我编写了一个独立的脚本,依赖于一些修改过的模块。目录结构如下所示:
client
setup.py
tsclient
__init__.py
tsup
utils.py
mutagen
__init__.py
blah.py
blah.py
...
colorama
__init__.py
blah.py
blah.py
...
目前,如果我只是将 usup
脚本符号链接到我的 ~/bin
目录,我可以直接调用该脚本并且它可以正常工作(一切都正确导入,没有问题)。
现在我想制作一个 setup.py 脚本,以便我可以分发它。我不知道该怎么做。这是我现在所拥有的:
setup(
name='tsclient',
version='1.0',
scripts=['tsclient/tsup'],
packages=['tsclient', 'tsclient.mutagen', 'tsclient.colorama'],
)
问题是我不能只在 tsup 脚本中执行 import mutagen
,因为它现在是 tsclient.mutagen
。如果我将导入更改为 from tsclient import mutagen
我会收到此错误(来自 mutagen 的 __init__.py
文件):
ImportError: No module named mutagen._util
我认为最好的解决方案不是通过诱变剂并更改“诱变剂”的每个实例并将其更改为“tsclient.mutagen”。这是我唯一的选择吗?
I wrote a standalone script depends on a few modified modules. the directory structure looks like this:
client
setup.py
tsclient
__init__.py
tsup
utils.py
mutagen
__init__.py
blah.py
blah.py
...
colorama
__init__.py
blah.py
blah.py
...
currently, if I just symlink the usup
script to my ~/bin
directory, I can invoke the script directly and it works with no problems (everything imports properly with no problems).
Now I want to make a setup.py script so I can distribute it. I can't figure out how to do it. Here is what I have now:
setup(
name='tsclient',
version='1.0',
scripts=['tsclient/tsup'],
packages=['tsclient', 'tsclient.mutagen', 'tsclient.colorama'],
)
The problem is that I can't just do import mutagen
in the tsup script because it's now tsclient.mutagen
. If I change the import to say from tsclient import mutagen
I get this error (from mutagen's __init__.py
file):
ImportError: No module named mutagen._util
I don't think the best solution is to go through mutagen and change every single instance of "mutagen" and change it to "tsclient.mutagen". Is this my only option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您确实需要编辑诱变剂才能使其发挥作用。
幸运的是,Python 2.5 及更高版本的语法可以准确支持您正在做的事情。
请参阅 http://docs.python.org /whatsnew/2.5.html#pep-328-absolute-and-relative-imports 。
假设诱变剂当前表示,
如果您将其更改为表示
那么它将继续作为顶级包工作;如果需要的话,你可以将整个东西移动到一个子包中,它仍然可以工作。
(但是,如果您使用
setuptools
,则可以添加setup.py 中的 install_requires=
参数告诉 setuptools 您的软件包需要安装 mutagen,然后您的软件包可以直接import mutagen
。)Unfortunately you do need to edit mutagen to make this work.
Fortunately Python 2.5 and later have syntax to support exactly what you're doing.
See http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports .
Suppose mutagen currently says,
If you change it to say
then it will continue to work as a top-level package; and if needed you can move the whole thing into a subpackage and it will still work.
(However, if you are using
setuptools
, you can instead add ainstall_requires=
argument in setup.py to tell setuptools that your package requires mutagen to be installed. Then your package could justimport mutagen
directly.)