How to correctly set PYTHONPATH for Visual Studio Code Ask Question

How to correctly set PYTHONPATH for Visual Studio Code Ask Question

How do I correctly set up the $PYTHONPATH variable for my workspace in VisualStudio Code?

Background Information

I have installed two versions of GNURadio:

  1. GNURadio version 3.7.11 installed by the Linux Mint package manager in /usr/lib/python2.7/dist-packages/gnuradio

  2. GNURadio version 3.7.13.4 installed by PyBOMBS in /home/tejul/Documents/gr13/default/lib/python2.7/dist-packages/gnuradio (my prefix directory is ~/Documents/gr13/default)

I can use the newer version of GNURadio version only after I run the setup_env.sh script (which -- among other things -- adds /home/tejul/Documents/gr13/default/lib/python2.7/dist-packages to $PYTHONPATH) and then start python in the terminal

tejul@Wacom:~/Documents/gr13/default$ ls
bin  etc  include  lib  libexec  setup_env.sh  share  src
tejul@Wacom:~/Documents/gr13/default$ source ./setup_env.sh 
tejul@Wacom:~/Documents/gr13/default$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from gnuradio import gr
>>> gr.version()
'3.7.13.4'
>>> 

Without modifying the $PYTHONPATH python -- naturally -- imports the older version of GNURadio.

I want to write, run, and debug python scripts for the new version of GNURadio in the VisualStudio Code. I've been trying to understand the selection of python interpreters, workspaces, and environments for VSCode.

As far as I understand it, the VSCode workspace setting python.pythonPath is not to be confused with the environment variable $PYTHONPATH. python.pythonPath is the path to the python interpreter used for debugging or running the code, while $PYTHONPATH is the environment variable which python uses to search for modules.

It looks like PyBOMBS did not install its own python interpreter into my prefix directory. So I need to use VSCode with my normal python interpreter located in /usr/bin/python2.7. So redefining VSCode's python.pythonPath or selecting another python interpreter would not help me.

I need to let VSCode use my own version of the environment variable $PYTHONPATH which would tell my regular python interpreter to import modules preferably from /home/tejul/Documents/gr13/default/lib/python2.7/dist-packages.

Problem

Following the documentation, I have created my own .env file in the workspace directory which sets the order of preference for locations from which python should import the modules. Alas, it has no effect on the python interpreter.

VSCode が $PYTHONPATH 設定を無視していることを示す

Can you see anything that I am doing wrong here? I have also tried:

  • Setting the PYTHONPATH to one folder level higher, i.e. /home/tejul/Documents/gr13/default/lib/python2.7, this did not help
  • Calling the variable $PYTHONPATH instead of PYTHONPATH, this did not help
  • Restarting VSCode after each change of the .env file, this did not help
  • Using double quotes around the path string, e.g. PYTHONPATH="/home/tejul/Documents/gr13/default/lib/python2.7:/usr/lib/python2.7", this did not help

ベストアンサー1

I have a situation that I believe is relatively common. I want a script to import a module from another directory. My python project is laid out as follows:

~/project/
  |
  |---modules/
        |
        |---mod.py
  |---scripts/
        |---script.py

in script.py, I have from modules import mod. So my PYTHONPATH needs to be set to ~/project/ (something that PyCharm does automatically).

VSCode is a great editor, but everywhere else, it falls short, in my opinion. This is a perfect example of that.

I create a default launch.json file to "run the current file". A "cwd": "${fileDirname}" line has to be added to make things work like they do in PyCharm (FYI, a list of the built-in variables can be found here).

Debugging

For debugging (the "play" button on the sidebar, or the F5 key), the PYTHONPATH set in launch.json or your .env file takes effect. Note that in the .env file, you cannot use variables such as ${workspaceRoot}, but you can easily append or insert to the path by using the proper separator for your platform (; for Windows and : for everyone else).

Because I want to take advantage of that variable, I put this in my launch.json:

    "env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}

(Thanks to @someonr for the suggestion to use ${pathSeparator}.)

It appears that you can prepend/append to whatever is inherited from the environment (this is not true for settings.json; see below).

This will also work for the hotkey Ctrl+F5 (run without debugging).

For reference, here's the full file, which replicates what PyCharm does automatically:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}",
            "env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}
        }
    ]
}

Run in terminal

If I hit the "play" button that appears on the top right of the editor window (when a python file is the active tab), it will not work. This runs the current file in a terminal, which doesn't pay attention to launch.json at all. To make that work, you have to define PYTHONPATH in a settings.json file, by adding this:

    "terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"}

(Note there are different values for each platform.) If you've selected a python interpreter (e.g. from a virtual environment), you will already have a settings.json file in the .vscode directory. Mine looks like this:

{
    "python.pythonPath": "/Users/me/project/venv/bin/python3",
    "terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"}
}

PYTHONPATHファイル経由で継承された に値を追加または挿入することはできませんsettings.json。 は 1 つの文字列のみを受け取り、区切り文字は解析しません。 したがって、 を使用して値を取得できたとしても${env:PYTHONPATH}、それを使って何もすることはできません。

さらに、現在の作業ディレクトリを設定することはできません。 で設定できそうに見えますが、"terminal.integrated.cwd": "${workspaceFolder}"機能しません。したがって、スクリプトのいずれかがツリー内の場所を基準としたパスを使用して何かを行う場合、そのスクリプトは機能しません。作業ディレクトリはプロジェクト ルートになります。

ファイルに変更を加える場合は、settings.json統合ターミナルを終了して再起動する必要があることに注意してください。

リンティング

launch.jsonに関して に対して何をしてPYTHONPATHも には影響がなく、にカーソルを置いて F12 キーを押すとファイルが開くにもかかわらず、pylintには赤い下線が付きます。 lint 設定を調べてみると、 のデフォルトにはが含まれています。この動作を pylint で再現するには、 に以下を追加します。from modules import modmodmypy--ignore-missing-importssettings.json

    "python.linting.pylintArgs": [
        "--disable=F0401"
    ] 

これを回避しなければならないのは残念ですが、最初にインポート ステートメントを記述するときには、オートコンプリートが非常に役立ちます。

結論

VSCode には多くのレイヤーがあり、連携して動作させるのは困難です。複数の環境が浮遊しているようです。結局のところ:

  1. 現在の作業ディレクトリを現在のファイルを含むパスに設定できないため、「ターミナルで実行」できません。
  2. は統合ターミナルとは異なる環境で実行され、 によって制御されるため、を設定PYTHONPATHすることはできません。そのため、インポート エラーを無視するように指示することしかできません。pylintlaunch.jsonpylint
  3. F5での実行は、PYTHONPATHファイル.envまたはlaunch.json

おすすめ記事