哪些脚本会进入Python包的bin文件夹?

2024-05-29 04:40:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我从《艰难学习Python》和其中一个练习中学到了Python包:

Put a script in the bin directory that you can run

对我来说,这似乎有点模糊。我不确定什么样的脚本会进入bin文件夹。The Hitchhiker's Guide to Packaging

put into bin any scripts you’ve written that use your package and which you think would be useful for your users. If you don’t have any, then remove the bin directory.

但我仍然想知道里面会有什么样的剧本。所以,我知道这听起来像一个愚蠢的问题,但有人能给我一个例子,什么时候,为什么一个人会把“脚本”放在他们的包的bin文件夹?


Tags: therunin脚本文件夹youyourbin
2条回答

例如Django's project creatingScrapy's project creating、django-admin.py和scrapy都是bin文件夹中的脚本。

通过检查几乎基于python的工具,您可以获得更多的示例。

我自己最近刚通过了LPTHW的Ex46。和你一样,我也被剧本搞糊涂了。如果另一个答案对你来说太超前了,我最后只写了一个简单的“hello world”脚本:

#!/usr/bin/env python

from test3 import printstring
printstring.printstring("test script working")
print "test over"

我将该文件命名为testscript3.py(*注意,我后来了解到,如果它是一个真正的脚本,并且我希望它看起来像一个系统命令,那么关闭.py文件扩展名会更方便)

我的文件test3.py是这样的:

def printstring(s='you did not provide string'):
    print s

以下是我在尝试让这个过程发挥作用时学到的一些新东西:

  • 那个#!symbol有时发音为shebang,简单的解释是,这一行的命令告诉shell使用python运行脚本。如果不使用“.py”文件扩展名,则脚本的用户不需要关心运行脚本所需的解释器。见wikipedia shebang article

  • 我运行了以下命令来打包发行版:

    python setup.py sdist

  • 完成之后,我可以通过运行

    sudo pip安装测试3-0.1.tar.gz

  • 我担心的一件事是脚本文件的权限。不过,我注意到distutils在打包(将模式更改为755或其他)时会处理这个问题。

你可以找到我的whole project for this example on github

相关问题 更多 >

    热门问题