简体   繁体   English

从不同目录导入模块时导入错误

[英]Import Error while importing module from different directory

I have a project directory structure like below我有一个如下的项目目录结构

|project 
|-__init__.py
 |-src 
  |- __init.py__
  |- features
   |- __init.py__
   |- clean_data.py
 |-notebooks 
  |- notebook.ipynb 

在此处输入图像描述

The main directory is called project under which I have two directories- src and notebooks.主目录称为 project,在该目录下我有两个目录 - src 和 notebooks。

I want to import the module clean_data.py under features directory (which is under src) in my notebook.ipynb file.我想在我的 notebook.ipynb 文件中导入功能目录(在 src 下)下的模块 clean_data.py。

I tried this:我试过这个:

from ..src.features import clean_data 

since all directories are serving as package with init.py file in each of them.因为所有目录都作为 package 在每个目录中都有 init.py 文件。

But it throws an error.但它会引发错误。 Have spent quite a lot of effort in trying to figure this out but not sure why I am getting the error.花了很多精力试图弄清楚这一点,但不确定为什么会出现错误。 As per this article too, I seem to be accessing the module correctly也根据这篇文章,我似乎正在正确访问该模块

mportError                               Traceback (most recent call last)
<ipython-input-23-11fd29e06b4c> in <module>()
----> 1 from ..src.features import clean_data

ImportError: attempted relative import with no known parent package

This is a part of my code, look at this:这是我的代码的一部分,看看这个:

from domain_pricing.domains import *
from domain_pricing.conversion_rate import *

I'm importing domains.py and conversion_rate.py from domain_pricing folder.我正在从domain_pricing文件夹中导入domains.pyconversion_rate.py

What you should do is:你应该做的是:

from src.features import clean_data
from src.data import another_module

You do not need .你不需要. or .. as Unix-Based systems pathing directories...作为基于 Unix 的系统路径目录。 You need to call the folder directly.您需要直接调用该文件夹。

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

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