Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError when I am trying to import package

Tags:

python

my_controller.py is as follow:

from models import Person

david = Person('David')

And my project structure is

app
├── controller
│   └── my_controller.py
│   
└── models
    └── __init__.py
    └── person.py

I must do something wrong because I keep get

ModuleNotFoundError: No module named 'models'    

What is the correct way to import class Person to my_controller.py?

PS I am working on ubuntu

like image 626
Moyshe Zuchmir Avatar asked Dec 19 '25 09:12

Moyshe Zuchmir


1 Answers

You need to use relative import

from . import models

Or it's better to import models that you will user, since it won't visually collide with django.db.models.

from django import forms
from .models import VolunteerBasicInfo

class BasicInfoCollectionForm(forms.ModelForm):

    class Meta:

        model = VolunteerBasicInfo
        ...

You also don't need to user brackets with class Meta.

like image 70
BILAL ASLAM Avatar answered Dec 21 '25 23:12

BILAL ASLAM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!