Git Product home page Git Product logo

Comments (2)

chrisyeh96 avatar chrisyeh96 commented on September 23, 2024 1

Great question! Here is the short answer to your specific question:

  1. Assuming that your terminal's current path is at mod/, you can run python -m unittest tests/test_module.py.
  2. If your terminal's current path is anywhere else, you will see errors. For example, if your terminal's current path is at mod/tests/, then running python -m unittest test_module.py will fail.

Here's the longer explanation. The directory structure you described is as follows:

mod/
    module.py
    tests/
        test_module.py (contains "import module")

The usage of the -m flag for the Python interpreter is something I did not really address in my guide, because it introduces some of its own complications. Below is a quick primer.

Suppose that the terminal is currently at some directory dir/. Then, the following command starts a Python interpreter with sys.path[0] set to the empty string "" which refers to the current path /path/to/dir/.

/path/to/dir/> python -m <module_name>

Therefore, in case 1 described above (/path/to/mod/> python -m unittest tests/test_module.py), the mod/ directory is in sys.path, so test_module.py is able to successfully call import module.

However, in case 2 (/path/to/mod/tests/> python -m unittest test_module.py), mod/ is not in sys.path, so the call to import module fails.

from chrisyeh96.github.io.

kylepw avatar kylepw commented on September 23, 2024

Thanks a lot @chrisyeh96 for the thorough response! Makes more sense now.

from chrisyeh96.github.io.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.