脚本中的 Python 导入模块不起作用

问题描述 投票:0回答:1
import json
import os
import django
import openai
import sys

# Ensure the DJANGO_SETTINGS_MODULE is set to your project's settings module.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tastify_backend.settings")
django.setup()

from recipe.models import Ingredient, IngredientCategory, MeasuringUnit, Recipe

print(Ingredient.objects.all().select_related('ingredient_category').filter(ingredient_category=1))

模型导入不起作用,有以下情况:

这是错误回溯,以便您可以检查我缺少的内容。

Traceback (most recent call last):
  File "/Users/patrickvogele/Desktop/tastify_backend/recipe/scripts/generate_ingredients.py", line 9, in <module>
    django.setup()
  File "/Users/patrickvogele/Desktop/tastify_backend/tastify_env/lib/python3.9/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/patrickvogele/Desktop/tastify_backend/tastify_env/lib/python3.9/site-packages/django/conf/__init__.py", line 102, in __getattr__
    self._setup(name)
  File "/Users/patrickvogele/Desktop/tastify_backend/tastify_env/lib/python3.9/site-packages/django/conf/__init__.py", line 89, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/patrickvogele/Desktop/tastify_backend/tastify_env/lib/python3.9/site-packages/django/conf/__init__.py", line 217, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'tastify_backend'

这是为什么?

python django import settings
1个回答
0
投票

可能您的任何 PATH 目录中都不存在名为“tastify_backend”的模块。如果它位于您的桌面上并且“~/Desktop/tastify_backend”不在您的 PATH 列表中,那么您的问题可能是 Python 没有看到此模块。

© www.soinside.com 2019 - 2024. All rights reserved.