ModuleNotFoundError and ImportError even beeing right

六眼飞鱼酱① 提交于 2021-02-17 07:09:41

问题


Hello, thanks for your time.

i'm trying to import models on seeders.py.

Can someone please tell me what am i doing wrong, i've done this a hundred times and tried every single way:

from winners.models import Player
or
from ..models import Player

models.py:


from django.db import models

class Player (models.Model):
    name = models.CharField(max_length=100)
    sex = models.CharField(max_length=9, choices=sex_choices)
    age = models.PositiveIntegerField()
    height = models.DecimalField(max_digits=3, decimal_places=2)
    weight = models.PositiveIntegerField()
    team = models.CharField(max_length=120)

by the way i've just started on linux may i havent set a envy variable right?


回答1:


Try importing sys module.

from sys import path

path.append(path_to_dir_with_module) # <- it might be loaded from ENV

# after this import your models

import models

If the issue is that python PATH does not contain path to your package, it should help.




回答2:


In short:

from .models import Player

would work fine.

In detail:

In Django default relative path points to the parent folder. i.e. all the files that are in the same folder can be imported directly into each other. and dot(.) prefix is used to donate the parent of the current folder. Hence the correct code will be:

from .models import Player

  


来源:https://stackoverflow.com/questions/66223221/kali-linux-python-not-beeing-able-to-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!