简体   繁体   English

将 Python package 添加到 VSCode 终端的路径

[英]Adding Python package to path for VSCode terminal

I'm writing python classes in a package using VScode.我正在使用 VScode 在 package 中编写 python 类。 I want to add this package's parent directory to the Python path when using the VScode Terminal so I can import the package (regardless of the directory of the file that's being run).我想在使用 VScode 终端时将此包的父目录添加到 Python 路径,这样我就可以import package(无论正在运行的文件的目录如何)。

I've tried a .env file and Workspace settings without success.我尝试了.env文件和 Workspace 设置但没有成功。

Ultimately I want to run doctests on the classes using the terminal, and for that the terminal needs to be able import the package.最终我想使用终端在类上运行文档测试,为此终端需要能够导入 package。

.env File .env 文件

I have a Workspace.我有一个工作区。 I first tried adding a .env file as follows (note I'm using Python 3 Anaconda on Windows ):我首先尝试按如下方式添加.env文件(注意我在 Windows 上使用 Python 3 Anaconda ):

PYTHONPATH=C:\\MyPython;${PYTHONPATH}

(I've tried single & double back slashes and forward slashes, nothing works). (我尝试过单双反斜杠和正斜杠,没有任何效果)。

When I run a script ( test.py ) in the terminal containing this:当我在包含以下内容的终端中运行脚本( test.py )时:

print(os.environ.get('PYTHONPATH'))

I just get back None .我刚回来None

I did try setting a system wide PYTHONPATH environment variable in Windows, which then shows up, but the C:\MyPython is not added to it.我确实尝试在 Windows 中设置系统范围的 PYTHONPATH 环境变量,然后出现,但 C:\MyPython 没有添加到其中。 I don't want to have to add/change the system PYTHONPATH every time I open a different Workspace!我不想每次打开不同的工作区时都添加/更改系统 PYTHONPATH!

Workspace Settings工作区设置

I then tried adding Workspace settings in the MyProject.code-workspace file:然后我尝试在MyProject.code-workspace文件中添加 Workspace 设置:

{
    "folders": [
        {
            "path": "C:\\MyPython"
        }
    ],
    "settings": {
        "terminal.integrated.cwd": "C:\\MyPython",
        "terminal.integrated.env.windows": "C:\\MyPython"
    }
}

Again this didn't work.这又没用。

File Structure文件结构

My file structure is as follows:我的文件结构如下:

C:\MyPython
    .env
    MyProject.code-workspace
    test.py
    Pkg\
        __init__.py
        Class1.py
        Class2.py

If I use the green triangle button ("Run File in Python Terminal") to run test.py then that file's directory ( C:\MyPython ) gets added to sys.path and everything works (eg import Pkg.Class1 works).如果我使用绿色三角形按钮(“在 Python 终端中运行文件”)运行test.py ,则该文件的目录( C:\MyPython )被添加到sys.path并且一切正常(例如import Pkg.Class1工作)。

However if I run Class2.py (which includes import Pkg.Class1 in the code and doctest) then instead the directory C:\MyPython\Pkg gets added to sys.path and it can't find and import the Pkg package and the doctest fails.但是,如果我运行Class2.py (在代码和 doctest 中包含import Pkg.Class1 ),则将目录C:\MyPython\Pkg添加到sys.path并且它无法找到并导入Pkg package 和doctest失败。

As such I want to add C:\MyPython to the python path, regardless of the directory of the file that is being run.因此,无论正在运行的文件的目录如何,我都想将C:\MyPython添加到 python 路径。

Of course, I could just add all tests to test.py and run that, but I really just want to run the doctests in the class I'm working on, rather than have to run all the tests every time (and flip to another file to do it).当然,我可以将所有测试添加到test.py并运行它,但我真的只想在我正在处理的 class 中运行 doctest,而不是每次都必须运行所有测试(然后切换到另一个文件来做到这一点)。

It seems like this should be easy, but I can't get it to work!看起来这应该很容易,但我无法让它工作!

Any ideas?有任何想法吗?

PS: I haven't included the.py code for the test or classes since it's irrelevant to the problem, it's the import that fails. PS:我没有包含测试或类的 .py 代码,因为它与问题无关,是import失败。 Ultimately I can see that the required directory appears neither in sys.path nor in os.environ.get('PYTHONPATH') and that's why the import fails.最终我可以看到所需的目录既没有出现在sys.path中,也没有出现在os.environ.get('PYTHONPATH')中,这就是导入失败的原因。

So two things.所以有两件事。 One, Python is not designed for you to execute files contained within packages, so you're somewhat going against its design trying to make this work.一,Python 不是为您执行包中包含的文件而设计的,因此您在尝试使其工作时有点违背它的设计。 It would be better to do something like use Python's -m flag to do what you want, eg python -m Pkg.Class2 .最好使用 Python 的-m标志来做你想做的事情,例如python -m Pkg.Class2 That way you don't have to manipulate your paths to run a module contained in your package.这样您就不必操纵路径来运行 package 中包含的模块。

Two, you were very close in getting what you were after with your settings, but you accidentally used a string instead of an object for specifying your environment variables for your terminal.第二,您非常接近您的设置,但您不小心使用了字符串而不是 object 来为终端指定环境变量。 What you wanted was:你想要的是:

"terminal.integrated.env.windows": {"PYTHONPATH": "C:\\MyPython"}

the VScode terminal is same as the windows one, so you can manually add it. VScode 终端与 windows 一样,可以手动添加。 press winkey+S and type "env", select "Edit the system environment variables".按winkey+S并输入“env”,select“编辑系统环境变量”。 select "environment variables". select“环境变量”。 edit the variable named "path" on both the user and the system one.在用户和系统上编辑名为“path”的变量。 type the path you wanna add in it.键入您要添加的路径。 remember to restart all the terminal after editing.记得在编辑后重新启动所有终端。

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

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