简体   繁体   English

VSCode - 在终端和 pythonpath 中运行 Python 文件

[英]VSCode - Run Python File in Terminal and pythonpath

I'm trying to understand the behaviour of VSCode in relation to running python scripts stored within sub packages of a project.我试图了解 VSCode 与运行存储在项目子包中的 python 脚本相关的行为。 If I have a project structure like the following:如果我有一个像下面这样的项目结构:

proj/
  util/
  main/hello.py
test/

If hello.py imports from proj.util - and I use 'Run Python File in Terminal' to execute the script it fails with Module not found (proj.util) - because by default the project top level directory is not added to pythonpath.如果 hello.py 从 proj.util 导入 - 并且我使用“在终端中运行 Python 文件”来执行脚本,它会因找不到模块 (proj.util) 而失败 - 因为默认情况下项目顶级目录未添加到 pythonpath。 If I create an .env file in the root and add a definition for PYTHONPATH to include my project top level directory this file is equally not used when running via the option above.如果我在根目录中创建一个 .env 文件并添加 PYTHONPATH 的定义以包含我的项目顶级目录,则在通过上述选项运行时同样不会使用该文件。

If I create a default set of debug launches and use the 'debug-Python current file in terminal' - this will read the .env file and works as expected.如果我创建一组默认的调试启动并使用“终端中的 debug-Python 当前文件” - 这将读取 .env 文件并按预期工作。

PyCharm automatically adds the top level project directory to sys.paths when running a script in the terminal - shouldn't VSCode provide such an option or am I missing something?在终端中运行脚本时,PyCharm 会自动将顶级项目目录添加到 sys.paths - VSCode 不应该提供这样的选项还是我遗漏了什么?

Current version is as follows, although I see the same behaviour under Windows.当前版本如下,尽管我在 Windows 下看到了相同的行为。

Version: 1.30.1
Commit: dea8705087adb1b5e5ae1d9123278e178656186a
Date: 2018-12-18T22:23:23.072Z
Electron: 2.0.16
Chrome: 61.0.3163.100
Node.js: 8.9.3
V8: 6.1.534.41
OS: Linux x64 4.19.12-arch1-1-ARCH

When you have Python run your hello.py , PVSC is using the terminal to run python proj/main/hello.py .当您让 Python 运行您的hello.py ,PVSC 正在使用终端运行python proj/main/hello.py To Python that is the same as running python hello.py from the proj/main directory which means to the interpreter not even seeing the packages that hello.py is contained within (hence why it has no concept of proj.util ).对于 Python 来说,这与从proj/main目录运行python hello.py相同,这意味着解释器甚至看不到hello.py包含在其中的包(因此它没有proj.util概念)。

The .env file isn't used because once again the file is just a direct execution in the terminal and neither your terminal or Python reads the .env file.不使用.env文件,因为该文件只是在终端中直接执行,您的终端或 Python 都不会读取.env文件。 But when you use the debugger we get to specify details like using your .env file and hence why the debugger sets PYTHONPATH as you expect.但是,当您使用调试器时,我们可以指定诸如使用.env文件之类的详细信息,因此调试器会按照您的预期设置PYTHONPATH

Just add these lines to the top of the file which you want to execute只需将这些行添加到要执行的文件的顶部

if __name__ == "__main__":
    import os
    import sys
    sys.path.append(os.getcwd())

These script add the directory, where file in, to your path when executing directly which __name__ == "__main__" means.这些脚本在直接执行时将文件所在的目录添加到您的路径中,其中__name__ == "__main__"意味着。

TLDR: Start your VS Code with the correct PYTHONPATH TLDR:使用正确的 PYTHONPATH 启动你的 VS Code

My setup: Set project specific PYTHONPATH from .env automatically with direnv ( https://direnv.net/ ) when changing into project directory.我的设置:更改为项目目录时,使用 direnv ( https://direnv.net/ ) 自动从 .env 设置项目特定的 PYTHONPATH。 This also sets the needed virtual environment for the project.这也为项目设置了所需的虚拟环境。 Start VS Code from command line, so that it uses the correct environment variables.从命令行启动 VS Code,以便它使用正确的环境变量。

Alternative you could create a shell script setting the right environment variables and starting VS Code.或者,您可以创建一个 shell 脚本来设置正确的环境变量并启动 VS Code。

Disclaimer: I use absolute imports in my packages, this is not tested with relative imports.免责声明:我在我的包中使用绝对导入,这没有使用相对导入进行测试。

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

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