如何设置 Visual Studio Code Python 扩展的根目录? [英] How to set the root directory for Visual Studio Code Python Extension?

查看:66
本文介绍了如何设置 Visual Studio Code Python 扩展的根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 VSCode Python 扩展 (ms-python.python) 运行和调试我的项目没有问题,但由于 python 子项目根目录不是整个项目目录,所有从我的项目导入源用红色下划线并列在 problems 中,因此 Go to definition 和一些类似的功能无法正常工作.我如何告诉 IDE 我的项目的起点在哪里:

I have no trouble running and debugging my project with VSCode Python Extension (ms-python.python), but since python sub-project root directory is not the whole project directory, all imports from my sources are underlined with red color and are listed in the problems and so Go to definition and some similar features don't work properly. How can I tell the IDE where's the start point of my project:

Whole Project path:
  docs
  server
    entities
      user.py
      customer.py
  env
  viewer
  db

server 目录是导入路径的起始位置:

The server directory is where the imports path are started from:

from entities.user import User

推荐答案

您可以创建一个 .env 文件:

You can create a .env file with:

PYTHONPATH=server

这将根据需要将您的 server 文件夹添加到 PYTHONPATH.

That will add your server folder to PYTHONPATH as needed.

(您可能需要重新启动 VSCode 才能正确考虑 PYTHONPATH.)

(You may need to restart VSCode for it to take PYTHONPATH into account correctly.)

编辑以澄清...

在 repo 根目录下创建一个名为 .env 的文件,例如your_repo/.env.

Create a file named .env under the repo root e.g. your_repo/.env.

也在您的消费代码所在的文件夹下创建文件,而不是在 repo root 下,似乎可以工作,例如your_repo/service/.env.

Also creating the file under the folder where your consuming code is, instead of under repo root, seems to work e.g. your_repo/service/.env.

有关详细信息,请参阅有关环境变量定义文件的文档.

For more details, see documentation on environment variable definition files.

对我来说,这无需重新启动 VSC 即可工作,也许这是更新的 VSC 和扩展版本的问题.

For me this worked without restarting VSC, perhaps this is a matter of newer VSC and extensions versions.

这篇关于如何设置 Visual Studio Code Python 扩展的根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆