Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing module not working

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)

enter image description here

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

like image 732
Peni Avatar asked Aug 01 '17 08:08

Peni


People also ask

Why can't I import modules in Python?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.


1 Answers

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.

like image 194
Cédric Julien Avatar answered Oct 05 '22 23:10

Cédric Julien