0

I've created an application inside my django project called SeedData. I've added then a file called import_data.py where I will be reading csv files and adding them to my database.

-SeedData
--__pycache__
--migrations
--__init__.py
--admin.py
--apps.py
--import_data.py
-- etc..

But whenever i do the following:

from Product.models import Product
from Category.models import Category
from SubCategory.models import SubCategory
from Brand.models import Brand
import pandas as pd

I get this error

File "C:\Users\...\SeedData\import_data.py", line 9, in <module>
    from Product.models import Product
ModuleNotFoundError: No module named 'Product'

It's the same for Category, SubCategory and Brand. I'd also like to say that the project is fully functional and all the mentioned projects above works perfectly fine.

  • 1
    Is there a django app named `Products`? (It's conventional to use lowercase names for modules). How are you running `import_data.py`? Custom commands or scripts using django models should usually be implemented as django management commands. This will ensure that database operations etc. work properly. https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/ – Håken Lid Mar 21 '21 at 12:06
  • Yes there is an app called Product. Also, I'm running import_data from the terminal. I'll check dmc right now thank you. – Hussein Khamis Mar 21 '21 at 12:14
  • 1
    If you are running it from the SeedData directory, the import probably fails because the django project root is not in sys.path. Using a management command will fix that. Your project root will be sys.path if you run the command using the project root as working directory `python manage.py my_command`. Alternatively you can add the project root directory to PYTHONPATH, for example. https://stackoverflow.com/questions/16525117/how-do-i-add-my-project-to-sys-path-on-windows-trying-to-use-disqus-export-p This lets you use `django-admin my_command` from any working directory. – Håken Lid Mar 21 '21 at 12:33
  • So I moved my script to this command feature django allows us to do. I'm now able to read files and import them. Thank you a ton buddy! – Hussein Khamis Mar 21 '21 at 12:55

0 Answers0