简体   繁体   English

在Python中,如何从具有相对路径的祖父母文件夹中导入?

[英]In Python, how can I import from a grandparent folder with a relative path?

Imagine a folder structure as follows: 想象一下一个文件夹结构,如下所示:

project/
    grandparent.py
    folder1/
        parent.py
        folder2/
            sibling.py
            current.py

If I am in current.py I can import from the other files using relative paths as follows: 如果我在current.py ,则可以使用以下相对路径从其他文件导入:

from .sibling import *
from ..parent import *

How can I import from grandparent.py ? 如何从grandparent.py导入?

(I've tried ...grandparent and ../..grandparent ) (我尝试过...grandparent../..grandparent

Create a Python Package 创建一个Python包

As a means to ensure some level of security - so that Python modules cannot access areas where they are not welcome - importing from parents or grandparents is generally prohibited... unless you create a package . 作为确保某种程度的安全性的一种手段-以便Python模块无法访问不受欢迎的区域-通常禁止从父母或祖父母那里导入... 除非您创建软件包

Luckily, in Python, creating a package is crazy-easy . 幸运的是,在Python中创建软件包非常容易 You simply need to add a __init__.py file in each folder/directory that you want to treat as part of the package. 您只需要在要作为软件包一部分的每个文件夹/目录中添加__init__.py文件。 And, the __init__.py file doesn't even need to contain anything . 而且, __init__.py文件甚至不需要包含任何内容 You just need the (potentially empty) file to exist. 您只需要存在(可能为空)文件。

For instance: 例如:

#current.py

from folder1.grandparent import display

display()

#grandparent.py
def display():
    print("grandparent")

# ├── folder1
# │   ├── __init__.py
# │   ├── folder2
# │   │   ├── __init__.py
# │   │   └── folder3
# │   │       ├── __init__.py
# │   │       └── current.py
# │   └── grandparent.py

Next Steps 下一步

This is not in the OP's question, but highly related and worth mentioning: If you import a directory instead of a module (file), then you are importing the __init__.py file. 这不是OP的问题,而是高度相关的且值得一提:如果您导入目录而不是模块(文件),那么您就是在导入__init__.py文件。 Eg, 例如,

import folder1

actually performs an import of the __init__.py file in the folder1 directory. 实际上是在folder1目录中执行__init__.py文件的导入。

Lastly, the double-underscore is used so often, it's shortened to dunder . 最后,双下划线经常被使用,它被缩短为dunder So when speaking, you can say "dunder init" to refer to __init__.py . 因此,在讲话时,您可以说“ dunder init”来引用__init__.py

current.py current.py

import os
import sys
FILE_ABSOLUTE_PATH = os.path.abspath(__file__)  # get absolute filepath
CURRENT_DIR = os.path.dirname(FILE_ABSOLUTE_PATH)  # get directory path of file
PARENT_DIR = os.path.dirname(CURRENT_DIR)  # get parent directory path
BASE_DIR = os.path.dirname(PARENT_DIR)  # get grand parent directory path
# or you can directly get grandparent directory path as below
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

sys.path.append(BASE_DIR)  # append the path to system
import grandparent
from folder1 import parent  # this way you can import files from parent directory too instead of again appending it to system path

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

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