在应用中,我创建了一个scrapers文件夹,里面有两个文件:__init__.py
和asana.py
。在 asana.py 中,当我尝试运行 asana.py
导入的模型时,我尝试导入模型,但出现以下错误 ModuleNotFoundError
,
最后我不明白为什么我不能运行 python asana.py
并得到一个错误 ModuleNotFound。但我尝试了不同的方法。将 Asana
类导入到 tests.py 中。
tests.py
from projects.scrapers.asana import Asana
URL = "https://app.asana.com/aprojecturl"
projects = Asana(URL, "23123123", "keyw")
projects.setProjectsIntoDatabase()
在tests.py中调用并运行类然后它工作了吗?没有未找到模块的错误...但仍然不明白为什么我不能执行 python asana.py
并获得未找到模块的错误?
Django 结构
--- projects
--- models.py
--- scrapers
---__init__.py
asana.py
asana.py
from projects.models import Project
class Asana:
def __init__(self, url, team, keyword=None):
self.url = url
self.projectKeyWord = keyword
self.team = team
self.rawData = []
self.projects = []
def setAsanaData(self):
res = requests.get(self.url, headers={"Authorization": "Bearer 0/"}).json()
self.rawData = res["data"]
def setProjects(self):
if self.projectKeyWord:
for project in self.rawData:
if project["name"].startswith(self.projectKeyWord):
project["team"] = self.team
self.projects.append(project)
def getProjects(self):
self.setAsanaData()
self.setProjects()
return self.projects
def setProjectsIntoDatabase(self):
self.getProjects()
for project in self.projects:
defaults = {"name": project["name"]}
Project.objects.update_or_create(pid=project["gid"], defaults=defaults)
URL = "https://app.asana.com/aprojecturl"
projects = Asana(URL, "23123123", "keyw")
projects.setProjectsIntoDatabase()
为什么我不能导入模型?