Python的家当和换行符 [英] Python shebang and newlines

查看:188
本文介绍了Python的家当和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在Linux上和bash Python脚本的可执行文件,一是使用家当

To make a Python script executable on Linux and bash, one uses the shebang

#! /usr/bin/env python

的脚本的第一行。如果这条线将发生在Windows风格的换行\\ r \\ n(回车 - 换行)结束,而不是Unix风格的换行符\\ n,那么庆典将无法通过Python间$ p来运行该脚本$ PTER。

as the first line of the script. If this line happens to end with the Windows-style newline \r\n (carriage return - line feed), instead of the Unix-style newline \n, then bash will not be able to run the script through the Python interpreter.

我在Python做跨平台开发。当我工作在Windows,自然换行符是\\ r \\ n。当我在Linux下工作,自然是换行的\\ n。由于我使用的水银进行版本控制,这将是强制执行包含家当脚本文件使用\\ n换行的最佳方式?

I am doing cross-platform development in Python. When I work in Windows, the natural newline is \r\n. When I work in Linux, the natural newline is \n. Given that I use Mercurial for version control, what would be the best way to enforce the use of the \n newline in the script file containing the shebang?

推荐答案

通常的做法是不是对自己产生的外部包装脚本可言,但在指定它们的 setup.py ,让他们包安装过程中生成。

The common approach is not to generate the outer wrapper scripts yourself at all, but specify them in your setup.py and let them be generated during package installation.

如果你做一些这样的:

setup(
    entry_points = {
        "console_scripts": [
            "script_name": "your.module.name:main",
        ],
    }
)

......那么,在安装时,一个包装命名为 SCRIPT_NAME 将会生成并安装,并适当配置到您当前的平台上运行(家当线和所有)。这使得在使用中没有实际意义尾行的字符。

...then, on installation, a wrapper named script_name will be generated and installed, and configured appropriately to run on your current platform (shebang line and all). This makes the end-of-line characters in use moot.

这篇关于Python的家当和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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