简体   繁体   中英

Python unittest - The file specified must still be importable as a module?

I am reading Python unittest command line interface docs . It says:

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.

I know that a *.py file is a module. So what is the meaning of:

The file specified must still be importable as a module.

There are actually three different ways to define a module in Python:

  • A module can be written in Python itself.
  • A module can be written in C and loaded dynamically at run-time, like the re (regular expression) module.
  • A built-in module is intrinsically contained in the interpreter, like the collections module.

A module's contents are accessed the same way in all three cases: with the import statement.

All you need to do to build a module and make it importable is create a file that contains legitimate Python code and then give the file a name with a .py extension. That's it!

No special syntax or voodoo is necessary.

You can find more on modules in Python's official docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM