0

I have a project directory structure like below

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

enter image description here

The main directory is called project under which I have two directories- src and notebooks.

I want to import the module clean_data.py under features directory (which is under src) in my notebook.ipynb file.

I tried this:

from ..src.features import clean_data 

since all directories are serving as package with init.py file in each of them.

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
Baktaawar
  • 7,086
  • 24
  • 81
  • 149
  • Which version of python are you using? – Jure C. Oct 26 '20 at 10:45
  • 1
    3.7.8. Using a conda environment an opening a notebook in that environment and trying to import a module from a different directory – Baktaawar Oct 26 '20 at 10:58
  • Have you tried using `from project.src.features import clean_data`? – rturrado Oct 26 '20 at 11:02
  • yes. It then says no Module named project. – Baktaawar Oct 26 '20 at 11:04
  • I have tried similar relative path in other projects and it works fine. I have no clue why this is not working this time. I have spent a lot of time to figure this out. No luck – Baktaawar Oct 26 '20 at 11:06
  • This may help: https://stackoverflow.com/q/16981921/260313 – rturrado Oct 26 '20 at 11:06
  • one of the ans mentions that the script shouldn't be in same package directory. My jupyter notebook from where I am calling the script is in another directory in parent dir than where the module is. Module is under src package. Notebook is under notebooks dir. Its not in same package – Baktaawar Oct 26 '20 at 11:29

1 Answers1

0

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.

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. You need to call the folder directly.

Saeed
  • 3,255
  • 4
  • 17
  • 36
  • what u r mentioning is absolute path. I have written a relative path. Both should work. None work for me. However if i use absolute path, then I would have to put whole path from project.src.features import clean_data Have tried this also, doesn't work – Baktaawar Oct 26 '20 at 10:59
  • That's what is working in my project, but including relative path for me didn't work. If that didn't work, then try importing `project.src.features` path. If none worked, I think I don't know more solutions. I shared my solution. – Saeed Oct 26 '20 at 11:04