简体   繁体   English

在Windows中为项目设置相对pythonpath(Visual Studio Code)

[英]Setting relative pythonpath for project in Windows (Visual Studio Code)

I'm trying to set up an environment for Python project to help out a friend (developing on different OS). 我正在尝试为Python项目建立一个环境,以帮助一位朋友(在不同的OS上开发)。 I want to set environment variables per project in the .env file, but ultimately it fails to recognize Pythonpath. 我想在.env文件中为每个项目设置环境变量,但最终它无法识别Pythonpath。

The goal is to set the /api subfolder of my currently opened folder, but ir doesn't recognize the relative path. 目标是设置我当前打开的文件夹的/ api子文件夹,但是ir无法识别相对路径。 Writing there absolute path (as in screenshot below) sort of works - it recognizes the custom library and all others, but on debugging it fails on other relative paths in the code (or so it appears) 在此处编写绝对路径(如下面的屏幕快照所示)的工作原理-它可以识别自定义库和所有其他库,但是在调试时,它在代码中的其他相对路径上失败(或者如此)

这是我的.env文件的屏幕截图-我尝试设置的相对和绝对路径

Any advice? 有什么建议吗? Thanks. 谢谢。

One approach is to avoid the whole environment setup to begin with and define your dependencies in source. 一种方法是避免整个环境设置从一开始就定义源代码中的依赖项。 This works when your repo has all you need. 当您的存储库满足您的所有需求时,此方法将起作用。

Consider the project structure like this 考虑这样的项目结构

proj
   /api
       api_source.py
   /utils
       utils_source.py
   /tests

api_source.py api_source.py

import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
import utils.utils_source as utils_source

It's a bit messy but I like this approach in that I do not have to setup any environment wherever I clone my repo, at least not for the self contained dependencies within the repo itself. 有点混乱,但是我喜欢这种方法,因为无论我在哪里克隆我的仓库都不必设置任何环境,至少不需要为仓库本身内部的自包含依赖项而设置。

Usually I'll do this path appending in the Main script and structure all my imports in the dependencies to rely on a fixed path prefix in the path list that way I do this operation once and all the dependent modules run with it. 通常,我会在Main脚本中添加此路径,并在依赖项中构造所有导入,以依赖路径列表中的固定路径前缀,这样我就可以执行此操作一次,并且所有依赖模块都将与之一起运行。

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

相关问题 在Visual Studio C ++中设置PYTHONPATH环境变量 - Setting PYTHONPATH environment variable in Visual Studio C++ 如何在Visual Studio Code中将python或PySpark模块添加或附加到PYTHONPATH - How to add or append python or PySpark modules to PYTHONPATH in Visual Studio Code 在 Windows 10 上设置 Python(在 Visual Studio 代码中) - Setting Up Python (in Visual Studio Code) on Windows 10 适用于 Windows 和 Linux 的 VS Code PYTHONPATH - VS Code PYTHONPATH for Windows and Linux Visual Studio Code 不建议相对导入 - Visual Studio Code is not suggesting relative imports 如何持久化Eclipse Pydev项目的PYTHONPATH设置? - How to persist PYTHONPATH setting of an Eclipse Pydev project? VSCode 未在 windows 的 WSL 模式下的工作区中设置 PYTHONPATH - VSCode not setting PYTHONPATH in workspace in WSL mode in windows Visual Studio Code 忽略 settings.json 字段:python.pythonPath - Visual Studio Code ignores settings.json field: python.pythonPath 如何设置Visual Studio以尊重PYTHONPATH中的文件夹? - How to setup Visual Studio to respect folders in the PYTHONPATH? 根据项目类型在Visual Studio Code中设置制表符空间样式 - Setting tab-space style in Visual Studio Code… accordingly to the project type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM