简体   繁体   English

从子文件夹导入主文件(不是另一个模块)

[英]import main file (not another module) from a subfolder

I want to import "main" from a subfolder. 我想从子文件夹导入“ main”。 Therefore every subfolder contains a __init__.py (but not "source", and its actual name is "Tracks Comparer"). 因此,每个子文件夹都包含一个__init __。py(但不包含“源”,其实际名称是“轨道比较器”)。 My folders structure is like this: 我的文件夹结构是这样的:

source\
  GPX\
    engine.py  (contains Engine class)
  main.py
    from GPX.engine import Engine
  Tests\
    Integration Tests\
      GPX\
        engineTest.py
      mainTest.py

I want use "main" from mainTest.py , so I tried: 我想使用mainTest.py中的“ main” ,所以我尝试了:

from ... import main 

but it does'n work. 但是没有用 Error: Attempted relative import in non-package 错误: 尝试在非软件包中进行相对导入

I use Visual Studio and with "Test Explorer" all tests run except for mainTest.py. 我使用Visual Studio,并与“测试资源管理器”一起运行所有测试,除了mainTest.py。 For example in engineTest.py there is: 例如,在engineTest.py中有:

from GPX.engine import Engine

and not something like this: 而不是这样的:

from ...GPX.engine import Engine   # this not works

and in mainTest.py the simple import main works too. 在mainTest.py中,简单的import main可以工作。

To be more exact launching tests with Visual Studio works also for mainTest, BUT the import of Engine from GPX.engine fails (Error: engine module not found ), I imagine because it refers to the GPX folder in the "Tests" folder. 为了更精确地启动Visual Studio的测试也可用于mainTest,但是从GPX.engine导入Engine失败(错误: 未找到引擎模块 ),我想是因为它引用了“ Tests”文件夹中的GPX文件夹。 For these reasons, I think the unittest.run() is "called" from root (source folder) when Visual Studio launch tests. 由于这些原因,我认为Visual Studio启动测试时会从根(源文件夹)“调用” unittest.run()。

To solve the problem I want to use relative paths (in tests files), but it doesn't work, as I said. 为了解决这个问题,我想使用相对路径(在测试文件中),但是它不起作用,正如我所说。

What's wrong in my relative path import? 我的相对路径导入有什么问题? Is this the right way for doing the job? 这是做这项工作的正确方法吗? I'm using Python 2.7. 我正在使用Python 2.7。


Solution for Attempted relative import in non-package : (in mainTest.py) 在非包中尝试相对导入的解决方案:(在mainTest.py中)

import os, sys
sys.path.append(os.path.join("..", ".."))
import main

Solution for engine module not found : (in mainTest.py) 找不到引擎模块的解决方案:(在mainTest.py中)

#sys.path.append(os.path.join("..", ".."))
sys.path.insert(0, os.path.join("..", ".."))   
# <-- this change set "source" earlier than "Integration Tests" in path.

Assuming you are currently in Integration Tests folder? 假设您当前在Integration Tests文件夹中? What about this: 那这个呢:

from os import path
import sys
currentDirectory = path.dirname(__file__)
sys.path.append(path.join(currentDirectory, '../../')) # This will get you to source
import main

Relative imports don't work at the file system level, so what you want to do can't be done with relative imports as you described. 相对导入在文件系统级别上不起作用,因此您要执行的操作不能像您所描述的那样使用相对导入。

It sounds like you are having issues with implicit relative imports (in mainTest when you import GPX it implicitly imports .GPX instead of GPX as a root module). 听起来您在隐式相对导入方面遇到了问题(在mainTest当您导入GPX它隐式导入.GPX而不是GPX作为根模块)。 You can turn off this behavior by adding this line at the top of all your test files: 您可以通过在所有测试文件的顶部添加以下行来关闭此行为:

from __future__ import absolute_import

This will make python import GPX from sys.path instead of .GPX . 这将使python从sys.path而不是.GPX导入GPX

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

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