进口”无法解决(Pylancereportmissingimports)
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')
import django
django.setup()
#FAKE POP SCRIPT
import random
from first_app.models import Topic, AccessRecord, Webpage
from faker import Faker
fakegen = Faker()
topics = ['Search', 'Social', 'Market', 'News', 'Games']
def add_topic():
t = Topic.objects.get_or_create(top_name = random.choice(topics))[0]
t.save()
return t
def populate(N=5):
for entry in range(N):
#Get the topic for the entry
top = add_topic()
#Create the fake data for that entry
fake_url = fakegen.url()
fake_date = fakegen.date()
fake_name = fakegen.company()
#Create the new webpage entry
webpg = Webpage.objects.get_or_create(topic = top,url = fake_url, name = fake_name)[0]
#Create a fake access record for that webpage
acc_rec = AccessRecord.objects.get_or_create(name = webpg, date = fake_date)[0]
if __name__ == '__main__':
print("populating script!")
populate(20)
print("populating complete!")
该代码执行,但什么也没做。我已经尝试卸载Faker并再次安装,安装旧版本,我也尝试使用PIP和Anaconda安装,但这些都无法使用。我会提供任何帮助。
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')
import django
django.setup()
#FAKE POP SCRIPT
import random
from first_app.models import Topic, AccessRecord, Webpage
from faker import Faker
fakegen = Faker()
topics = ['Search', 'Social', 'Market', 'News', 'Games']
def add_topic():
t = Topic.objects.get_or_create(top_name = random.choice(topics))[0]
t.save()
return t
def populate(N=5):
for entry in range(N):
#Get the topic for the entry
top = add_topic()
#Create the fake data for that entry
fake_url = fakegen.url()
fake_date = fakegen.date()
fake_name = fakegen.company()
#Create the new webpage entry
webpg = Webpage.objects.get_or_create(topic = top,url = fake_url, name = fake_name)[0]
#Create a fake access record for that webpage
acc_rec = AccessRecord.objects.get_or_create(name = webpg, date = fake_date)[0]
if __name__ == '__main__':
print("populating script!")
populate(20)
print("populating complete!")
The code executes but does nothing. I've already try uninstall faker and install it again, install older versions, I tried to install with pip and anaconda as well but none of these worked. I would appretiate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
的凹痕如果__name__ =='__ main __':
是错误的。正确的方法在填充
函数之外如下:用它有效:
输出
The indentation of
if __name__ == '__main__':
is wrong. The right way is outside thepopulate
function like the following:Testing with minimal-reproducible-example it works:
The output
由于您使用了错误的Python解释器,因此可能不可见。
步骤1:选择正确的Python解释器
ctrl+shift+shift+p
(或cmd+shift+shift+shift+p
mac上的P )在VSCODE中打开命令调色板。app-vubfewl
在您的情况下)。它应该看起来像c:\ users \ jefde \ .virtualenvs \ app - vubfewl \ scripts \ python.exe
。步骤2:重新加载或重新启动VScode
有时需要重新加载以正确识别Python环境中的更改:
ctrl+shift+shift+shift+p
打开命令调色板再次并键入“重新加载窗口”,然后选择它以刷新VSCODE环境。The import may not be visible because you used the wrong Python Interpreter.
Step 1: Select the Correct Python Interpreter
Ctrl+Shift+P
(orCmd+Shift+P
on Mac) to open the Command Palette in VSCode.app--vubfewl
in your case). It should look something likeC:\Users\jefde\.virtualenvs\app--vubfewl\Scripts\python.exe
.Step 2: Reload or Restart VSCode
Sometimes VSCode needs a reload to properly recognize the change in the Python environment:
Ctrl+Shift+P
to open the Command Palette again and type "Reload Window" and select it to refresh the VSCode environment.