简体   繁体   中英

Nosetest & import

I am pretty new to Python. Currently I am trying out PyCharm and I am encountering some weird behavior that I can't explain when I run tests.

The project I am currently working on is located in a folder called PythonPlayground. This folder contains some subdirectories. Every folder contains a init .py file. Some of the folders contain nosetest tests.

When I run the tests with the nosetest runner from the command line inside the project directory, I have to put "PythonPlayground" in front of all my local imports. Eg when importing the module called "model" in the folder "ui" I have to import it like this:

from PythonPlayground.ui.model import *

But when I run the tests from inside Pycharm, I have to remove the leading "PythonPlayground" again, otherwise the tests don't work. Like this:

from ui.model import *

I am also trying out the mock framework, and for some reason this framework always needs the complete name of the module (including "PythonPlayground"). It doesn't matter whether I run the tests from command line or from inside PyCharm:

with patch('PythonPlayground.ui.models.User') as mock:

Could somebody explain the difference in behavior to me? And what is the correct behavior?

I think it happens because PyCharm have its own "copy" of interpreter which have its own version of sys paths where you project's root set to one level lower the PythonPlayground dir. And you could find preferences of interpreter in PyCharm fro your project and set proper top level. ps. I have same problems but in Eclipse + pydev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM