0

I want to import a file from subdir and I get an error like that:

ModuleNotFoundError: No module named 'special'

│   
├── constants.py
├── crawler.py
├── case.py ===================>>>> I working on this file
├── special
    ├── __init__.py
    └── wantToImport.py =================>>>I want to import this file

My case.py like that:

from special.wantToImport import ImportClass

And my wantToImport.py file like that:

class ImportClass:

    def mydefination(self):
        #Some codes here
 

I want to use mydefinitioon function on my case.py file But I cannot import this file. Why Im getting this error? How can I solve this?

akasaa
  • 1,282
  • 4
  • 13
  • 33
  • Does this answer your question? [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – Allan Wind Apr 08 '21 at 11:07

2 Answers2

1

You should tell Python that your module's path is inside the current directory (using a .):

from .special.wantToImport import ImportClass
0

Try creating a __init__.py file in the directory where you are working and use it to refer your module.

│
root   
├── constants.py
├── crawler.py
├── case.py ===================>>>> I working on this file
├── __init__.py
├── special
    ├── __init__.py
    └── wantToImport.py =================>>>I want to import this file

inport it using

from root.special.wantToImport import ImportClass
phileinSophos
  • 362
  • 4
  • 22