0

In Python2, I have the following directory structure:

Project
|____Tests
     |____tester.py
|____Scripts
     |____run_test_1.py
     |____run_test_2.py

In tester.py, I have a class called Tester, which takes in some arguments, and then runs a test (details are not important). In run_test_1.py and run_test_2.py, I create a Tester object, with a certain set of arguments, and run this test.

But I am having difficulty with relative imports. In run_test_1.py, I have the line from Tests import tester. However, if I run python run_test_1.py from the command line, I receive the error No module named Tests. I think that this is because Python only searches the current directory for packages, so it cannot find my Tests directory.

So, what should I do instead? One solution is to move run_test_1.py up one level, so that it is in the Project directory. This works, because Python searches the current directory and it can then find Tests. But for my particular case, I want run_test_1.py to be in the Scripts directory.

Thank you.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
John Rowlay
  • 83
  • 1
  • 6

0 Answers0