【问题标题】:Python Project Issue: Import from parent folder or change project architecturePython 项目问题:从父文件夹导入或更改项目架构
【发布时间】:2012-04-03 02:47:12
【问题描述】:

我的 Python 项目有问题。我的项目为每个食谱解析不同的食谱网站,并将食谱数据写入文本文件。

我的问题:我无法导入位于父目录中的 python 文件/模块。该文件包含解析网页的通用函数。执行此操作时出现错误:“ValueError: Attempted relative import in non-package”

我的解决方案:我不知道我的解决方案是什么?我想我要么需要找到一种方法从父目录导入文件,要么更改我的项目架构,以便每个域解析脚本都位于同一目录中

项目架构:

Test Folder:  
    __init__.py   
    UtilityFunctions.py     # the file I am importing  
    PerformAllIndexing.py 
    all_recipes:
        __init__.py
        index.py            # imports ../UtilityFunctions.py
    simply_recipes:
       __init__.py
        index.py            # imports ../UtilityFunctions.py

每个子目录都包含一个名为 index.py 的文件,该文件可以提取特定网站的食谱名称、成分和说明,并将其写入文本文件。

我鼓励您下载我的项目以详细了解我的问题:http://www.mediafire.com/?ynup22oe8ofam21

您能告诉我如何从父目录导入 python 模块,或者如何更改我的项目架构以使其工作而无需重复代码(位于 UtilityFunctions.py 中的代码)?

【问题讨论】:

  • 感谢有组织的问题。但是你能发布你的导入声明是什么样的吗?不是每个人都想下载你的整个项目来找出答案。
  • 这个[问题][1]可以参考我的回答。我希望这会有所帮助。 [1]:stackoverflow.com/questions/714063/…

标签: python architecture project software-design


【解决方案1】:

只需将测试文件夹添加到 python sys.path,这样您就可以毫无问题地导入它。

import sys
# Add the Test Folder path to the sys.path list
sys.path.append('/path/to/test/folder/')

# Now you can import your module
from test_folder import UtilityFunctions

【讨论】:

  • 我相信相对导入也应该在这一点上起作用:from .. import UtilityFunctions
  • 要删除路径依赖,您可以附加到 sys 路径,如 sys.path.append(../Test_Folder)
【解决方案2】:

我发现相对导入和项目布局可能很难掌握!如果您按照以下步骤操作,您应该可以正常工作:

  • 首先,如果您使用的是 from __future__ import relative_imports。
  • 那么,为了简单起见,你需要这个布局

    Project Root Folder:
        Test_Folder:  
            __init__.py   
            UtilityFunctions.py 
            PerformAllIndexing.py 
            all_recipes:
                __init__.py
                index.py
            simply_recipes:
                __init__.py
                index.py
    

    请注意,Project Root Folder 中没有 __init__。

  • 然后,您需要确保 Project Root Folder 在您的 PYTHONPATH 中。
  • 现在,您的库中的相对导入应该可以正常工作,例如里面all_recipes.index:

    from .. import UtilityFunctions
    

    绝对导入从 Test Folder 开始,例如

    from Test_Folder import UtilityFunctions
    

但是,如果您只是尝试运行all_recipes.index,这不会起作用。当您运行脚本时,它需要绝对导入。把它想象成你正在编写一个库和一个使用该库的脚本。该库使用相对导入,但您无法使用python -m 正常运行它。您可以像这样运行脚本,但不能使用相对导入。所以你的脚本可能看起来像

    from Test_Folder.PerformAllIndexing import run
    run()

当您尝试运行单元测试时,这可能会导致问题,因为您并不总是希望运行整个套件。我对测试的建议是将其保存在 Project Root Folder 下的单独的 tests 文件夹中,并且只使用绝对导入。它还有助于使用 nose 等测试运行程序。

【讨论】:

  • 相对导入从 python2.5 开始可用。他必须在旧蟒蛇上才能需要未来的导入
  • 感谢您的信息,我以为它们只包含在 python3 中...我会更新我的答案。
【解决方案3】:

您的项目架构:

Test Folder:  
    __init__.py   
    UtilityFunctions.py     # the file I am importing  
    PerformAllIndexing.py 
    all_recipes:
        __init__.py
        index.py            # imports ../UtilityFunctions.py
    simply_recipes:
       __init__.py
        index.py            # imports ../UtilityFunctions.py

为了不摆弄PYTHONPATH 或sys.path,您最好将all_recipes/index.py 和simply_recipes/index.py 移动到项目的根目录,并相应地命名它们。

因此,您可以使用 import UtilityFunctions 轻松使用 UtilityFunctions(顺便说一句,您应该将其命名为 utility_functions)。

另请参阅:Import paths - the right way?

附:更改 sys.path 有时会导致奇怪的行为。例如,一个模块可以导入两次。 此外,您必须设置 IDE 以了解您对路径的修改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2021-01-03
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多