[英]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)
在此处编写绝对路径(如下面的屏幕快照所示)的工作原理-它可以识别自定义库和所有其他库,但是在调试时,它在代码中的其他相对路径上失败(或者如此)
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.