相关疑难解决方法(0)

在Python 2.6中不推荐使用BaseException.message

当我使用以下用户定义的异常时,我收到一条警告,即在Python 2.6中不推荐使用BaseException.message:

class MyException(Exception):

    def __init__(self, message):
        self.message = message

    def __str__(self):
        return repr(self.message)
Run Code Online (Sandbox Code Playgroud)

这是警告:

DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
self.message = message
Run Code Online (Sandbox Code Playgroud)

这有什么问题?我需要更改什么来摆脱弃用警告?

python exception deprecated

168
推荐指数
4
解决办法
8万
查看次数

如何检查python模块是否存在并且可以导入

我正在使用django的调试工具栏,如果两个条件为真,我想将它添加到项目中:

  • settings.DEBUGTrue
  • 模块本身存在

做第一个并不难

# adding django debug toolbar
if DEBUG:
    MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware',
    INSTALLED_APPS += 'debug_toolbar',
Run Code Online (Sandbox Code Playgroud)

但是如何检查模块是否存在?

我找到了这个解决方案:

try:
    import debug_toolbar
except ImportError:
    pass
Run Code Online (Sandbox Code Playgroud)

但由于导入发生在django的其他地方,我需要if/else逻辑来检查模块是否存在,所以我可以在settings.py中检查它

def module_exists(module_name):
    # ??????

# adding django debug toolbar
if DEBUG and module_exists('debug_toolbar'):
    MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware',
    INSTALLED_APPS += 'debug_toolbar',
Run Code Online (Sandbox Code Playgroud)

有办法吗?

python django python-import

25
推荐指数
1
解决办法
3万
查看次数

Python:有没有更好的方法来跳过测试模块?

我读了如何在运行时跳过整个Python unittest模块?这似乎不是一个非常简洁的方式

在Java中,您必须说的是@Ignore测试类,并忽略整个测试类

skip对于整个测试模块是否有更好的方法,或者我应该这样做

class TestMe():
  @unittest.skip
  def test_a(self):
    pass

  @unittest.skip
  def test_b(self):
    pass
  ...
Run Code Online (Sandbox Code Playgroud)

通过@unittest.skip在模块上添加所有测试

python

0
推荐指数
1
解决办法
148
查看次数

标签 统计

python ×3

deprecated ×1

django ×1

exception ×1

python-import ×1