Home Ask Login Register

Developers Planet

Your answer is one click away!

The6thSense February 2016

Importing modules from nested folder

I have a folder structure like this

|  |
|  |--test1
|  |--__init__.py


class Tries(object):
    def __init__(self):
        print "Test"


from done.test1 import Tries


ImportError                               Traceback (most recent call last)
<ipython-input-8-10953298e1df> in <module>()
----> 1 from done.test1 import Tries

ImportError: No module named done.test1 

I am not able to import modules from the nested folder. Is there any way to do this.


After Salva's answer I changed my structure like this

├── check.py
|--__init__.py(no content)
└── done
    ├── __init__.py(no content)
    └── test1
        └── __init__.py <-- this files contains your Tries class

Same error is thrown now also.


Salva February 2016

You need a file __init__.py in each directory you want it to be considered a package so you need it in both directories:

├── check.py
└── done
    ├── __init__.py
    └── test1
        └── __init__.py <-- this files contains your Tries class

Lam February 2016

In the following file/folder structure your code just works here:

├── check.py
└── done
    └── test1.py

When I run check.py it prints Test. I didn't use __init__.py though. What you described as __init__.py I made test1.py.

ikhtiyor February 2016

Try to import package done first

import done

If it doesn't work, probably you are running script from different folder than you specified (in this case main_folder)

From logs it seems like you are using IPython, in this case try to add your folder as module path

import sys
import done

Post Status

Asked in February 2016
Viewed 2,099 times
Voted 8
Answered 3 times


Leave an answer

Quote of the day: live life