如果从错误的解释器调用 python 脚本,如何快速失败?

发布于 2024-09-19 22:49:05 字数 431 浏览 1 评论 0原文

我从离开我雇主的人那里继承了一些 Python 脚本。有些是由 Jython 运行的,有些则不是。

我想将它们添加到 svn,但在此之前我想修改这些文件,以便如果从 python 运行“需要 Jython”文件,用户会收到类似“请使用 Jython 运行”的消息,然后程序退出。

(警告:我对 Python/Jython 不太熟悉。)

我希望最简单的方法是创建一个文件 require-jython.py ,其内容如下:

if runtime.name !=  'jython'
  print "Please run with Jython"
  exit(1) 

然后“include/require” ?这个文件(再说一次,我不是专家。请耐心等待)

任何人都可以为我阐明这些步骤吗?

I have inherited a few Python scripts from someone who has left my employer. Some are meant to be run from Jython, others are not.

I'd like to add them to svn, but before I do I want to modify these files so that if a "requires Jython" file is run from python, the user gets a message like "please run with Jython" and the program exits.

(Warning: I am not very familiar with Python/Jython.)

I expect the simplest way to do this is create a file require-jython.py with the contents like:

if runtime.name !=  'jython'
  print "Please run with Jython"
  exit(1) 

and then "include/require"? this file (again I'm not an expert. bear with me here)

Can anyone spell out the steps for me?

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

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

发布评论

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

评论(1

鹊巢 2024-09-26 22:49:05

我所看到的做法是尝试导入给定版本或实现独有的模块,如果该模块不存在,则引发 ImportError

想象一下 Jython(而不是 Python)有一个名为 special 的模块,然后添加:

# at the top of your module:
try:
   import special
except ImportError:
   raise ImportError("this script is meant to be used with Jython")
else:
   raise

请注意,您使 ImportError 异常更加明确,而不是简单地引发它(并且导致用户相信模块本身有问题,而不是告知解释器选择不当)。我会给你一个更具体的例子来说明要导入什么模块,但我对 Jython 一点也不熟悉。

换句话说,使用 ducktyping 进行模块导入:假设导入正确但失败一旦您找不到预期的行为(这就是 try 语句的用途)。

检查解释器的另一种方法是使用 sys 模块(在 Python 中 - 我不知道 Jython 是否有它):

>>> import sys
>>> print sys.subversion
('CPython', 'tags/r264', '75821M')

What I have seen done is to try to import a module exclusive to a given version or implementation, and raise ImportError if the module does not exist.

Imagine that Jython (and not Python) has a module called special, then you add:

# at the top of your module:
try:
   import special
except ImportError:
   raise ImportError("this script is meant to be used with Jython")
else:
   raise

Notice that you make the ImportError exception more explicit, as opposed to simply raising it (and lead the user to believe that there was a problem with the module itself, as opposed to informing that the interpreter was improperly selected). I would give you a more concrete example of what module to import, but I am not at all familiar with Jython.

In other words, use duck typing for the module import: assume the import was made correctly but fail as soon as you cannot find the expected behaviour (this is what the try statement is supposed to be used for).

Another way to check the interpreter is to use the sys module (in Python - I don't know if Jython has it):

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