ImportError:无法从部分初始化的模块“..”导入名称“..”(很可能是由于循环导入)

我有一个应用程序,其表单到目前为止一直运行良好。我不确定我做了什么导致这个错误,因为它已经工作了好几个月了。我尝试更改文件名,在其中导入模型,但没有任何效果我正在附加代码的相关部分:

这是我的models.py

class SingleEnd(models.Model):

file = models.FileField(upload_to='documents/')
email = models.CharField(max_length=100)

def __str__(self):
        return self.email

这是我的forms.py

    from app.models import SingleEnd
class SingleEndForm(forms.ModelForm):
    class Meta:
        #shows which model to use from models.py           
        model = SingleEnd
        #fields = '__all__'
        fields = ['file', 'email']

        labels = {
            'file': 'input your fasta/fastq file (min 3 sequences)',
            'email':'input your email to get a notification for your results in a timely manner',
        }
        widgets = {
            'email':forms.EmailInput(attrs={'class':'form-control'}),
            'file':forms.FileInput(attrs={'class':'form-control'}),
        }

这是我的意见.py

    #defines a view function    
def snippet_detail(request):
    #path to save inputs
    media_path = "/home/project/media/documents/"
    #path to save outputs
    result_path = '/home/project/media/results'
    max_timeout = 1*24*60*60 #days*hours*mins*secs

    #dictionary-like object that uses form data
    if request.method == 'POST':
        #use the form to upload form info (post) and files
        form = SingleEndForm(request.POST, request.FILES)
        if form.is_valid():
            #saves full form 
            form = SingleEndForm(file=request.FILES['file'])
            form.save()

            #changes file name if the name is the same
            file_name_final = form.file.name[10:]
            #final path
            file_path = media_path + file_name_final
        else :
            raise Http404("Form not entered correctly")
    #form used 
    form = Kraken2Form()
    #returns a web response (Html, image etc)
    return render(request, 'page.html', {'form': form})

这是我终端的完整回溯

    Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/.local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/.local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "/home/.local/lib/python3.8/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/home/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 398, in execute
    autoreload.check_errors(django.setup)()
  File "/home/.local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/.local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/.local/lib/python3.8/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/home/.local/lib/python3.8/site-packages/django/apps/config.py", line 304, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/project/app/models.py", line 29, in <module>
    from app.views import workflow
  File "/home/project/app/views.py", line 21, in <module>
    from .forms import SingleEndForm
  File "/home/project/app/forms.py", line 22, in <module>
    from app.models import SingleEnd
ImportError: cannot import name 'SingleEnd' from partially initialized module 'app.models' (most likely due to a circular import) (/home/project/app/models.py)

enter image description here

stack overflow ImportError: cannot import name '..' from partially initialized module '..' (most likely due to a circular import)
原文答案

相关问题