Windows での Python 開発用の VS Code は初めてで、pylint でパッケージが見つかりません。これは私のプロジェクトのディレクトリ構造です。
workspace/ <- This is VS Code workspace (E:\workspace)
.vscode/
launch.json
settings.json
project1/
mypackge/
__init__.py <- In here, I wrote: `import mypackage.first_sub_pkg`
first_sub_pkg/
__init__.py <- In here, I wrote: `from .second_sub_pkg.mymodule import MyClass`
second_sub_pkg/
__init__.py <- In here, I wrote: `from .mymodule import MyClass`
mymodule.py <- This module has class: `MyClass`
test_script/
mytest.py
project2/
etc.../
そして、次のような mytest.py スクリプト コードを書きました。
from mypackge.first_sub_package import MyClass
Pythonインタープリターに C:/Anaconda3/python.exe を使用しています
VS Code の右上にある ▷ (Run Python File in Terminal) のボタンをクリックすると、このエラー メッセージが表示されます。
PS E:\workspace> & c:/Anaconda3/python.exe e:/workspace/project1/test_script/mytest.py
Traceback (most recent call last):
File "e:/workspace/project1/test_script/mytest.py", line 1, in <module>
from first_sub_pkg.second_sub_pkg import MyClass
ModuleNotFoundError: No module named 'first_sub_pkg'
また、workspace/.vscode/launch.json を次のように追加しました。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"pythonPath": "${command:python.interpreterPath}",
"env": {
"PYTHONPATH": "${workspaceFolder};E:/workspace/project1"
}
}
]
}
そして、workspace/.vscode/settings.json は次のようになります。
{
"python.autoComplete.extraPaths": [
"E:/workspace",
"E:/workspace/project1",
"E:/workspace/project1/first_sub_pkg",
],
"python.pythonPath": "c:/Anaconda3/python.exe",
"terminal.integrated.shell.windows": "C:/windows/System32/WindowsPowerShell/v1.0/powershell.exe",
"python.linter": "pyLint",
"python.linting.pylintPath": "pylint"
}
そして、私の user settings.json ファイルは次のようなものです:
{
"python.autoComplete.extraPaths": [
"E:/workspace",
"E:/workspace/project1",
"E:/workspace/project1/first_sub_pkg",
]
}
このテスト スクリプトは Eclipse + pydev 環境で既に実行しましたが、問題なく実行できました。しかし、どういうわけか、VSC は私のモジュールをインポートできません。
Python を実行して「E:/workspace/project1」をシステム パス ( ) に追加するとうまくいくので、システム パスの問題のようですimport sys; sys.path.append('E:/workspace/project1');
が、問題の解決方法がわかりません。(Windows 設定にシステム変数を追加しても機能しませんでした)。
私は何を取りこぼしたか?誰か助けてください。2日間探しましたが、どこにも行きませんでした。