简体   繁体   English

如何从一个包中导入文件,而该包又从同一包中导入另一个文件

[英]How to import a file from a package which is importing another file from the same package

I've been working on a project where I have a file which needs to call a function from a file in a sub package/directory, which in turn is calling a function from another file in the same sub package. 我一直在一个项目中,我有一个文件,该文件需要从子包/目录中的文件调用函数,而该文件又从同一子包中的另一个文件中调用函数。 As such, I have a main file which is importing a sub file. 这样,我有一个主文件正在导入一个子文件。 This sub file is also importing another sub file which is in the same package. 此子文件还将导入同一包中的另一个子文件。

The first sub file has no issue whatsoever importing the second sub file. 第一个子文件导入第二个子文件没有任何问题。 The main file also has no issue importing the first sub file. 主文件也没有问题,导入第一个子文件。 However, when I put it all together and run the main file, Python thinks that the second sub file doesn't exist, which I find strange. 但是,当我将它们放在一起并运行主文件时,Python认为第二个子文件不存在,我觉得很奇怪。 I've simplified and visualised my problem with an example below: 我通过下面的示例简化并可视化了我的问题:

I have the following file hierarchy: 我具有以下文件层次结构:

test_package\
    __init__.py
    main_file.py
    test_sub_package\
        __init__.py
        subfile1.py
        subfile2.py

main_file code: main_file代码:

import test_sub_package.subfile1

subfile1 code: subfile1代码:

import subfile2

subfile2 code: subfile2代码:

def get_string():
    return ("Hello, World!")

So, I would expect main_file to import subfile2 via subfile1. 因此,我希望main_file通过subfile1导入subfile2。 However this doesn't seem to be the case because I get an error: 但是似乎不是这样,因为我得到一个错误:

Traceback (most recent call last):
  File "...\Test\main_file.py", line 1, in <module>
    import test_package.subfile1
  File "...\Test\test_sub_package\subfile1.py", line 1, in <module>
    import subfile2
ModuleNotFoundError: No module named 'subfile2'

I was a little surprised that I got this error before I even attempted to call the functionality in subfile2. 我什至在尝试调用subfile2中的功能之前就收到了此错误,我感到有些惊讶。 Either way, I'm confused why this doesn't work. 无论哪种方式,我都很困惑为什么这行不通。 Am I just doing something stupid here or am I trying to do something Python fundamentally doesn't support. 我只是在这里做一些愚蠢的事情,还是在做Python根本不支持的事情。 If anyone can give me a solution it would be most appreciated. 如果有人可以给我解决方案,将不胜感激。

I suspect this is probably a duplicate but I couldn't find an answer to my specific problem. 我怀疑这可能是重复的,但我找不到我特定问题的答案。 So, sorry in advance. 所以,对不起。

When you import a module into another module from the same directory you must use must use a relative import in subfile1.py you will need to write: 当您从同一目录将一个模块导入另一个模块时,必须使用必须在subfile1.py中使用相对导入,您将需要编写:

from . import subfile2

Note, that doesn't give subfile 1 access to get_string to use it in subfile1, you would need to either write subfile2.get_string() or import it directly with: 请注意,不给子文件1次访问get_string在subfile1使用它,你需要编写任何subfile2.get_string()或直接导入:

from .subfile2 import get_string

I have tried this out and it works, I hope this helps :) 我已经尝试过了并且可以正常工作,希望对您有所帮助:)

Note: that, if you are running a python script, and you need to import a module in that same directory, you can just say import module_name . 注意:如果您正在运行python脚本,并且需要在同一目录中导入模块,则可以说import module_name It makes a difference if it is a script you are running, or a module that is being used in some other script. 如果它是您正在运行的脚本,还是某个其他脚本中正在使用的模块,则有所不同。 For a detailed explanation as to why see here 有关为什么的详细说明,请参见此处

(I assume from your error message that you want to run main.py, if this is not the case you will need to change import test_sub_package.subfile1 to from . import test_sub_package.subfile1 ) (我从错误消息中假设您要运行main.py,如果不是这种情况,则需要将import test_sub_package.subfile1更改为from . import test_sub_package.subfile1

main file should be: 主文件应为:

from test_sub_package.subfile1 import get_string
get_string()

subfile1.py subfile1.py

import test_sub_package.subfile2

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

相关问题 Django从另一个包导入另一个文件 - Django importing another file from another package 如何从一个文件中导入变量,该变量可以在另一个文件中使用,而另一个文件又可以在同一文件中导入,而无需再次导入该文件 - How to import variables from a file which can be used in another file which is imported in the same file without importing the file again 从python中的不同包导入文件时出现相对导入错误 - relative import error while importing file from diffrent package in python 从另一个.py文件导入函数时未定义numpy包 - numpy package not defined when importing function from another .py file Python 导入系统,从一个模块导入 function,从另一个模块导入相同的 function - Python Import system, importing function from a module that imports the same function from another package Python-包名称相同时从文件导入 - Python - import from a file when a package has the same name Python在同一个包中的__init__.py中导入类 - Python Import class in __init__.py from file in same package Python 从另一个文件导出的 package 的子模块导入 - Python import from submodule of package exported by another file 从父目录导入文件(不是包) - Importing a file (not a package) from a parent directory 将文件从Python包导入到目录中 - Importing a file from a Python package in to a directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM