进口”无法解决(Pylancereportmissingimports)

发布于 2025-02-12 08:50:34 字数 1227 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2025-02-19 08:50:34

的凹痕如果__name__ =='__ main __':是错误的。正确的方法在填充函数之外如下:

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!")

它有效:

from faker import Faker

fakegen = Faker()

def populate(N=5):
    for _ in range(N):
        #Create the fake data for that entry
        fake_url = fakegen.url()
        fake_date = fakegen.date()
        fake_name = fakegen.company()
        print(fake_url, fake_date, fake_name)

if __name__ == '__main__':
    print("populating script!")
    populate(20)
    print("populating complete!")

输出

populating script!
https://www.jackson-allen.com/ 1987-05-26 Rogers, Adams and Keith
https://jefferson-smith.com/ 1976-01-05 Murphy-Smith
https://swanson-evans.info/ 1986-02-10 Martin-Fields
http://www.fleming-miller.com/ 2010-06-18 Tanner-Frost
https://higgins.net/ 2008-11-20 Mccoy-Jones
http://www.cunningham.net/ 2017-02-10 Kramer-Mccall
http://wright-mills.com/ 1973-08-14 Moran, Humphrey and Spears
https://diaz.com/ 2017-05-16 Thomas Ltd
http://klein-flores.com/ 1988-12-18 Gentry Group
http://www.potts.com/ 1986-01-29 King-Moore
https://perez.biz/ 1998-09-09 Robinson, Fowler and Callahan
https://khan.com/ 2016-06-17 Wells-Mcdonald
https://manning-garcia.com/ 2011-04-28 Jenkins, Crawford and Garcia
https://foster.com/ 2004-09-26 Rodriguez Ltd
https://www.wright.com/ 1987-10-06 Velasquez Group
http://www.davis.net/ 1985-07-28 Armstrong, Wood and Grant
http://www.blevins.biz/ 1984-03-10 Ross LLC
http://www.mcintyre.biz/ 1971-11-23 Daugherty-Jackson
http://thompson.com/ 1980-07-15 Payne, Powers and Wilson
https://www.thomas-mason.com/ 2002-08-09 Pacheco, Palmer and Hernandez
populating complete!

The indentation of if __name__ == '__main__': is wrong. The right way is outside the populate function like the following:

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!")

Testing with minimal-reproducible-example it works:

from faker import Faker

fakegen = Faker()

def populate(N=5):
    for _ in range(N):
        #Create the fake data for that entry
        fake_url = fakegen.url()
        fake_date = fakegen.date()
        fake_name = fakegen.company()
        print(fake_url, fake_date, fake_name)

if __name__ == '__main__':
    print("populating script!")
    populate(20)
    print("populating complete!")

The output

populating script!
https://www.jackson-allen.com/ 1987-05-26 Rogers, Adams and Keith
https://jefferson-smith.com/ 1976-01-05 Murphy-Smith
https://swanson-evans.info/ 1986-02-10 Martin-Fields
http://www.fleming-miller.com/ 2010-06-18 Tanner-Frost
https://higgins.net/ 2008-11-20 Mccoy-Jones
http://www.cunningham.net/ 2017-02-10 Kramer-Mccall
http://wright-mills.com/ 1973-08-14 Moran, Humphrey and Spears
https://diaz.com/ 2017-05-16 Thomas Ltd
http://klein-flores.com/ 1988-12-18 Gentry Group
http://www.potts.com/ 1986-01-29 King-Moore
https://perez.biz/ 1998-09-09 Robinson, Fowler and Callahan
https://khan.com/ 2016-06-17 Wells-Mcdonald
https://manning-garcia.com/ 2011-04-28 Jenkins, Crawford and Garcia
https://foster.com/ 2004-09-26 Rodriguez Ltd
https://www.wright.com/ 1987-10-06 Velasquez Group
http://www.davis.net/ 1985-07-28 Armstrong, Wood and Grant
http://www.blevins.biz/ 1984-03-10 Ross LLC
http://www.mcintyre.biz/ 1971-11-23 Daugherty-Jackson
http://thompson.com/ 1980-07-15 Payne, Powers and Wilson
https://www.thomas-mason.com/ 2002-08-09 Pacheco, Palmer and Hernandez
populating complete!
你又不是我 2025-02-19 08:50:34

由于您使用了错误的Python解释器,因此可能不可见。

步骤1:选择正确的Python解释器

  1. 打开命令调色板:按ctrl+shift+shift+p(或cmd+shift+shift+shift+p mac上的P )在VSCODE中打开命令调色板。
  2. 搜索“ python:选择解释器” :在命令调色板中键入此命令。
  3. 选择解释器:查找与您的虚拟环境相对应的解释器路径(app-vubfewl在您的情况下)。它应该看起来像c:\ users \ jefde \ .virtualenvs \ app - vubfewl \ scripts \ python.exe
  4. 选择它:单击此解释器,将其设置为Active Python解释器的VSCODE会话。

步骤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

  1. Open Command Palette: Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) to open the Command Palette in VSCode.
  2. Search for "Python: Select Interpreter": Type this command into the Command Palette.
  3. Choose the Interpreter: Look for the interpreter path that corresponds to your virtual environment (app--vubfewl in your case). It should look something like C:\Users\jefde\.virtualenvs\app--vubfewl\Scripts\python.exe.
  4. Select It: Click on this interpreter to set it as the active Python interpreter for your VSCode session.

Step 2: Reload or Restart VSCode

Sometimes VSCode needs a reload to properly recognize the change in the Python environment:

  • Reload Window: Use Ctrl+Shift+P to open the Command Palette again and type "Reload Window" and select it to refresh the VSCode environment.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文