Thursday, 15 March 2018

python - Importing modules from nested folder




I have a folder structure like this



main_folder
|
|--done
| |
| |--test1
| |--__init__.py

|
|---check.py


__init__.py:



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



check.py:



from done.test1 import Tries
Tries()


Error:



---------------------------------------------------------------------------

ImportError Traceback (most recent call last)
in ()
----> 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.



Edit:




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.


Answer



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

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...