Wednesday, 9 December 2020

How do I properly import python modules in a multi directory project?

I have a python project with a basic setup that looks like this:

imptest.py

utils/something.py
utils/other.py

Here's what's in the scripts:

imptest.py

#!./venv/bin/python

import utils.something as something
import utils.other as other

def main():
    """
    Main function.
    """

    something.do_something()
    other.do_other()

if __name__ == "__main__":
    main()

something.py

#import other

def do_something():
    print("I am doing something")


def main():
    """
    Main function
    """

    do_something()
    #other.do_other()

if __name__ == "__main__":
    main()

other.py

def do_other():
    print("do other thing!")

def main():
    """
    Main function
    """

    do_other()

if __name__ == "__main__":
    main()

imptest.py is the main file that runs and calls the utils functions occasionally for some things.

And as you can see, I have commented out some lines in "something.py" where I am importing "other" module for testing.

But when I want to test certain functions in something.py, I have to run the file something.py and uncomment the import line.

This feels like a bit of a clunky way of doing this.

If I leave the

import other

uncommented and run imptest.py, I get this error:

Traceback (most recent call last):
  File "imptest.py", line 5, in <module>
    import utils.something as something
  File "...../projects/imptest/utils/something.py", line 3, in <module>
    import other
ModuleNotFoundError: No module named 'other'

What's a better way of doing this?



from How do I properly import python modules in a multi directory project?

No comments:

Post a Comment