导入调用其他自定义模块的自定义模块时出错

时间:2019-04-24 16:53:27

标签: python python-3.x

我正在尝试导入一些依赖于其他定制模块的定制模块。文件夹结构类似于以下内容:

project
|_ App.py
|_ LinkedList
|    |_ LinkedList.py
|    |_ LLList.py
|    |_ Node.py
|_ Parser
|    |_ Parser.py
|_ Cursor
     |_ cursor.pkl
     |_ logs.txt

该脚本从App.py运行,除了许多第三方模块和标准模块之外,该脚本还导入所有这些自定义模块。目前,我有

LLList.py导入LinkedList.pyNode.py

LinkedList.py导入Node.py

Parser.py导入LLList.py

最终App.py导入Parser.py

运行App.py时,出现以下错误

ModuleNotFoundError: No module named 'Node'

我想在保持文件夹结构不变的情况下解决此问题。

为完整起见,代码:

App.py

# App.py
# -*- coding: utf-8 '*-

# Libraries
# Standard Libraries
from datetime import datetime
import json
import os
import sys
from typing import Dict, List, Union

# Environment Variables
PACKAGE_PARENT = ".."
SCRIPT_DIR = os.path.dirname(
    os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

# User Defined Libraries
import Parser.Parser as Parser

Parser.py

# Parser.py
# -*- coding: utf-8 -*-

# Libraries
# Standard Libraries
import argparse
import json
import os
import pickle
import sys
from typing import Dict, List

# Environment Variables
PACKAGE_PARENT = ".."
SCRIPT_DIR = os.path.dirname(
    os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

# User Defined Libraries
from LinkedList.LinkedList import LLList

LLList.py

# LLList.py

# Libraries
# Standard Libraries
import os
import pickle
import sys

# Environment Variables:
PACKAGE_PARENT = ".."
SCRIPT_DIR = os.path.dirname(
    os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

# User Defined Libraries
from LinkedList import LinkedList
from Node import Node

LinkedList.py

# LinkedList.py

# Libraries
# Standard Libraries
import os
import sys

# Environment Variables
PACKAGE_PARENT = ".."
SCRIPT_DIR = os.path.dirname(
    os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

# User Defined Libraries
from Node import Node

Node.py没有导入

过去,我的进口产品在Relative imports in Python 3上取得了成功,现在看来似乎行不通。

0 个答案:

没有答案