Python unittest指定的文件仍必须作为模块导入?

2024-09-27 23:23:28 发布

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

我正在读Python ^{} command line interface docs。它说:

Test modules can be specified by file path as well:

python -m unittest tests/test_something.py

This allows you to use the shell filename completion to specify the test module. The file specified must still be importable as a module. The path is converted to a module name by removing the .py and converting path separators into .. If you want to execute a test file that isn’t importable as a module you should execute the file directly instead.

我知道*.py文件是一个模块。那么,以下内容的含义是什么:

The file specified must still be importable as a module.


Tags: thetopathpytestyoubyas
1条回答
网友
1楼 · 发布于 2024-09-27 23:23:28

在Python中定义模块实际上有三种不同的方法:

  • 模块本身可以用Python编写
  • 模块可以用C编写并在运行时动态加载,就像re(正则表达式)模块一样
  • 内置模块本质上包含在解释器中,就像collections模块一样

在这三种情况下,访问模块内容的方式相同:使用import语句

要构建一个模块并使其可导入,只需创建一个包含合法Python代码的文件,然后为该文件指定一个扩展名为.py的名称。就这样

没有特殊的语法或巫毒是必要的

您可以在Python's official docs.中找到有关模块的更多信息

相关问题 更多 >

    热门问题