Home > database >  ModuleNotFound Error when trying to import variable from script
ModuleNotFound Error when trying to import variable from script

Time:08-04

I have a python script named "WhatsApp_to_Excel.py". There is a variable there that I need to use for a Django project I'm making. So I'm trying to import the module to the models.py file. This is my folder hierarchy-

enter image description here

Then I make my import like this-

enter image description here

But even though pycharm auto-completed the module's name, when I run the server I get the no module found error.

enter image description here

I'm running my code from a virtual environment, but I don't see how this can be the problem. I've tried to import the module in an absolute way, but it didn't work and it's rather weird that pycharm DOES recognize the module.

Does anybody Have any ideas on how to solve the problem? Would appreciate any help :-)

CodePudding user response:

Try to put that WhatsApp_to_Excel.py file in the same directory of models.py

CodePudding user response:

Thank you guys! I solved the problem using your advice-

  1. I moved my python script inside the folder with models.py (an init file was created automatically).

  2. I've imported a variable from my script using this code

    from .WhatsApp_to_Excel import TXT_PATH

And it works!!

  • Related