Reputation: 666
I have a django (but i think it's nor revelant here) project where I try to add a script i did before. So I put it in a subdirectory of my project, and i have this structure (I know it's a little bit of a mess at the moment but it won't stay exactly like that)
From views.py i want to import main.py (Especially the function excelToXml) . After searches on internet i found that code that i copied in views.py . If I undestood it right it add to the variable $PATH the directory parent of first_page and though, every subdirectory
CURRENT = os.path.dirname(os.path.abspath(__file__))
PARENT = os.path.dirname(CURRENT)
sys.path.append(PARENT)
from ExcelToXML.main import excelToXml
I also created a file __init.py__ in the directory ExcelToXML, this file is left empty.
However even I did all that i still get this error when i run the django server
File "c:\Users\CRA\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\django\bin\DevisVersOpen\DevisVersOpen\urls.py", line 18, in module
from first_page import views
File "c:\Users\CRA\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\django\bin\DevisVersOpen\first_page\views.py", line 13, in module
from ExcelToXML.main import excelToXml
ModuleNotFoundError: No module named 'ExcelToXML'
I didn't find any solution that I could understand on internet so I really don't know how to solve this
Upvotes: 11
Views: 97266
Reputation: 11
For python change the location of the import file and then import. It worked for me.
Location can be: C:\python 3.8\Lib
Upvotes: 0
Reputation: 27
Check this for details on what is init.py file What is __init__.py for?
The init.py file should be present in every directory and sub directories whose classes should be made visible for import. In your case, the I am suspecting that the parent directory does not have the init.py file. Add the file in the parent directory and import it as follows
import first_page.ExcelToXML
Upvotes: 2
Reputation: 425
Okay hello, the solution you found is (I think) a mess, you should read the official documentation about it https://docs.python.org/3.6/tutorial/modules.html#packages.
In short just add from .idea.main import excelToXml
.
If it does not work, rename .idea
folder to idea
(without the dot) and add in your views.py
this line :from idea.main import excelToXml
Upvotes: 0
Reputation: 80771
Your directory structure let me think that you should try to import like this :
from first_page.ExcelToXML.main import excelToXml
because the ExcelToXML is under the first_page module, so it is viewed as a submodule of first_page.
Upvotes: 9