简体   繁体   English

为什么我的代码在 pycharm 中有效,但在 Visual Studio 代码中无效?

[英]Why does my code work in pycharm but not in visual studio code?

My main problem is on the import of images in python.我的主要问题是在 python 中导入图像。

Here is the code that concerns it:这是与它有关的代码:

import pygame
import neat
import time
import os
import random
pygame.font.init()

WIN_WIDTH = 500
WIN_HEIGHT = 800


BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird2.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird3.png")))]
PIPE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "pipe.png")))
BASE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "base.png")))
BG_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bg.png")))

the error message is:错误信息是:

Traceback (most recent call last):
  File "c:\Users\Dell\Documents\Code Projects\Python\Machine learning\Floppy Bord\flappy_bird_noai.py", line 12, in <module>
    BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird2.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird3.png")))]
pygame.error: Couldn't open imgs\bird1.png

However in pycharm all works fine.但是在 pycharm 中一切正常。 Ive already tried changing the python interpreter.我已经尝试过更改 python 解释器。 Both use the anaconda base.两者都使用 anaconda 底座。 Thank you for the time reading this!感谢您花时间阅读本文!

Probably the file bird3.png is not available from the VS code project, make sure that the file is in the proper location. VS code 项目中可能没有文件bird3.png,请确保该文件位于正确的位置。

I know from my own personal experience this is due to the PYTHONPATH file not being set correctly in your venv.根据我自己的个人经验,我知道这是由于 PYTHONPATH 文件未在您的 venv 中正确设置。

Try using the following command in your vs code尝试在您的 vs 代码中使用以下命令

export PYTHONPATH=.导出 PYTHONPATH=。

That might fix the issue you are having.这可能会解决您遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM