1

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

piscvau's avatar
496
piscvau
asked 2023-06-24 09:27:18 -0500
Wingware Support's avatar
4k
Wingware Support
updated 2023-07-01 07:43:29 -0500
edit flag offensive 0 remove flag close merge delete

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)

Wingware Support's avatar Wingware Support (2023-06-26 06:25:50 -0500) edit

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.

piscvau's avatar piscvau (2023-06-26 15:03:06 -0500) edit

Does it work if you add this file to the Testing tool and debug from there instead?

Wingware Support's avatar Wingware Support (2023-06-27 09:09:27 -0500) edit

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).

piscvau's avatar piscvau (2023-06-28 04:14:51 -0500) edit

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.

Wingware Support's avatar Wingware Support (2023-06-28 07:49:03 -0500) edit

Hello I Have tried all combinations from the testmodule all the way "up" to the main directory of the projcet. I have done it by copying the filename from the menu and replacing the / with . and removing hte last .py. NOthing helps!... Would there be another way to add a package variable in the environment of the launch configuration? If I understand correctly this is what is required to have relative import working!...

piscvau's avatar piscvau (2023-06-28 09:17:53 -0500) edit

What is the name of the file? testfile.py should work but testfile.py will not -- because test-file is not a valid python name.

Wingware Support's avatar Wingware Support (2023-06-28 09:47:56 -0500) edit

I have the same problem with all my test files which are named tests_ AnyHow I do realize that I need to add environment variable and that therefore using launch configurations is handier than using python -m. So I am coming back to the problem : How to set up the parent package in a launch configuration so I can use relative imports without getting trapped with the no known parent package problem. I this a directory problem?

piscvau's avatar piscvau (2023-06-28 11:30:01 -0500) edit

Hello I strongly suspect that my wing configuration is broken. I tried to add more directories into the project properties but wing refuses. When I select a new directory to insert into the list of python Paths, the OK button is inefficient. Also when I need to add a new file in a launch configuration and select custom python path, I cannot select a directory : I get the message There is no remote host configuration for host . Do you want to create one?

FInally the display of files and directories in the right panel Project is weird. I have some directories which are listed twice.

For information, My django configuration is not a standard one. The manage.py is under a directory web and the project root is web.parent.parent I just submitted a bug report.

piscvau's avatar piscvau (2023-06-28 12:05:32 -0500) edit

What version of Wing do you have? If not the latest (9.1.1.3) please try that by downloading from our website and then doing Check for Updates from the Help menu. There have been a lot of fixes for remote development, which is why I'm asking this.

Wingware Support's avatar Wingware Support (2023-06-28 12:32:59 -0500) edit

Hello I just downloaded a the latest version and installed it. As far as the display of files It does not change anything. please note that I am not doing remote development but only local development. I will try to see if there is an improvement with this version.

piscvau's avatar piscvau (2023-06-28 13:36:36 -0500) edit
add a comment see more comments

1 Answer

1

Hello the new release 9.1.1.3 does solve the problem and python -m works.

piscvau's avatar
496
piscvau
answered 2023-07-01 02:31:23 -0500
edit flag offensive 0 remove flag delete link

Comments

Thanks for confirming this!

Wingware Support's avatar Wingware Support (2023-07-01 07:45:46 -0500) edit
add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer