简体   繁体   English

Nosetest和进口

[英]Nosetest & import

I am pretty new to Python. 我对Python很陌生。 Currently I am trying out PyCharm and I am encountering some weird behavior that I can't explain when I run tests. 目前,我正在尝试PyCharm,并且遇到一些奇怪的行为,在运行测试时无法解释。

The project I am currently working on is located in a folder called PythonPlayground. 我当前正在处理的项目位于名为PythonPlayground的文件夹中。 This folder contains some subdirectories. 此文件夹包含一些子目录。 Every folder contains a init .py file. 每个文件夹都包含一个init .py文件。 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. 当我从项目目录中的命令行使用鼻子测试运行器运行测试时,必须将“ PythonPlayground”放在所有本地导入文件的前面。 Eg when importing the module called "model" in the folder "ui" I have to import it like this: 例如,在“ ui”文件夹中导入名为“ model”的模块时,我必须像这样导入它:

from PythonPlayground.ui.model import * 从PythonPlayground.ui.model导入*

But when I run the tests from inside Pycharm, I have to remove the leading "PythonPlayground" again, otherwise the tests don't work. 但是,当我从Pycharm内部运行测试时,必须再次删除领先的“ PythonPlayground”,否则测试将无法正常工作。 Like this: 像这样:

from ui.model import * 从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"). 我也在尝试模拟框架,由于某种原因,该框架始终需要模块的完整名称(包括“ PythonPlayground”)。 It doesn't matter whether I run the tests from command line or from inside PyCharm: 无论是从命令行还是从PyCharm内部运行测试,都没有关系:

with patch('PythonPlayground.ui.models.User') as mock: 使用patch('PythonPlayground.ui.models.User')作为模拟:

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. 我认为发生这种情况是因为PyCharm具有自己的解释器“副本”,这些解释器具有其自己的sys路径版本,您在其中将项目的根目录设置为比PythonPlayground目录低一级的目录。 And you could find preferences of interpreter in PyCharm fro your project and set proper top level. 您可以在您的项目的PyCharm中找到解释器的首选项,并设置适当的最高级别。 ps. ps。 I have same problems but in Eclipse + pydev 我有同样的问题,但是在Eclipse + pydev中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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