Using relative import in a test file
I have a tests module which ends with if_name_ = '__main--' : import unittest unittest.main().
I would like to be able to use relative imports in the module. if I used a launch configuration I get a no parent package error, which is confirmed by a print at the top of the module where package is None. My understanding is that therefore I should launch it as a module with python -m When I select this option and add the name of the module as a run argument in the form of parent_package.module wing refuses to laucnh the module sayin it needs a dotted name.
Do you have a hint to launch this module as a main. I thank you in advance.
PS. I have a __init__.py in the same directory as my test module. and the parent.parent.parent directory is in the python Path
Comments
Are you trying to launch through the Testing tool or directly using the debugger? If you're using the Testing tool, it should be launching as a package module because __init__.py exists, but you can also change the Run As Package Module setting to Run As Package Module ... on the Testing page of the Project Properties dialog. Make sure that the parent (or grandparent) directory of the package is in the Python path.
If you're launching the debugger directly, it should work to use Debug > Debug Current File and then select Launch with 'python -m' ... in the pre-launch dialog box. There's no need to enter the package or module name.
Note that the if __name__ == '__main__': logic probably isn't correct if you're launching with python -m because the __name__ will be the name of module within the package so if the file in named testbasic.py, the __name__ ... (more)
Thank you for you answer and the remark about the if __name__==__main__ logic which is obviously wrong. I am using the debugger directly. I do Debug File and choose the python -m option . Then I get the following error message : some values are invalid. The module name for the file needs to be a dotted name. Please correct the values and try again.
Does it work if you add this file to the Testing tool and debug from there instead?
HelloI think I cannot do this. I am in a django project and want to debug a test-file with unittest. My understainding is that I need to configure file properties (right click on the file in the editor) and select from there the python -m option. And this is where the problem occurs (error message requiring a dotted name for the module).
I suspect you need a different dotted name. You said parent.parent.parent is on the Python Path so I'd expect you would need parent.parent.modulename as the dotted name for -m. I could be misunderstanding, though. If you can make a simple example to pack up and email us at support@wingware.com that might clarify it.