运行 Visual Studio 2017 年内的 pylint 不起作用

Running pylint within Visual Studio 2017 doesn't work

我的虚拟环境中安装了pylint,在解决方案中配置并激活了虚拟环境

当我通过命令提示符 运行 pylint(激活虚拟环境后)时,它工作:

C:\Users\AmosH\Documents\Workspace\local_gallery>pylint projects
Using config file C:\Users\AmosH\Documents\Workspace\local_gallery\.pylintrc
************* Module projects.admin
C:  1, 0: Missing module docstring (missing-docstring)
************* Module projects.apps
C:  1, 0: Missing module docstring (missing-docstring)
C:  4, 0: Missing class docstring (missing-docstring)
************* Module projects.models
C:  1, 0: Missing module docstring (missing-docstring)
C:  5, 0: Missing class docstring (missing-docstring)
C: 15, 4: Missing method docstring (missing-docstring)
...

-----------------------------------
Your code has been rated at 2.29/10

但是当我 运行 它在 Visual Studio 中时(右键单击项目 > Python > 运行 PyLint)它只是显示奇怪的警告:

Severity    Code    Description Project File    Line    Suppression State
Warning     No module named general\admin.py [F:fatal]  local_gallery   general\admin.py    1   
Warning     No module named download_some_images.py [F:fatal]   local_gallery   download_some_images.py 1   
Warning     No module named general\apps.py [F:fatal]   local_gallery   general\apps.py 1   
Warning     No module named general\management\commands\populate_db.py [F:fatal]    local_gallery   general\management\commands\populate_db.py  1   
Warning     No module named general\management\commands\__init__.py [F:fatal]   local_gallery   general\management\commands\__init__.py 1   
Warning     No module named general\management\__init__.py [F:fatal]    local_gallery   general\management\__init__.py  1   
Warning     No module named general\migrations\__init__.py [F:fatal]    local_gallery   general\migrations\__init__.py  1   
Warning     No module named general\models.py [F:fatal] local_gallery   general\models.py   1   
Warning     No module named general\templatetags\lg.py [F:fatal]    local_gallery   general\templatetags\lg.py  1   
Warning     No module named general\tests.py [F:fatal]  local_gallery   general\tests.py    1   
Warning     No module named general\views.py [F:fatal]  local_gallery   general\views.py    1   
Warning     No module named general\__init__.py [F:fatal]   local_gallery   general\__init__.py 1   
Warning     No module named local_gallery\renderers.py [F:fatal]    local_gallery   local_gallery\renderers.py  1   
Warning     No module named local_gallery\settings.py [F:fatal] local_gallery   local_gallery\settings.py   1   
Warning     No module named local_gallery\urls.py [F:fatal] local_gallery   local_gallery\urls.py   1   
...

每个文件都无法加载自己,或者类似这样的事情(请注意,每个模块都不会尝试加载其他模块,而是尝试加载自己,但会失败)。

当我双击任何警告时,我可能会收到这样的错误:

或另一个让我参考 "C:\Users\AmosH\AppData\Roaming\Microsoft\VisualStudio.0_e34de02d\ActivityLog.xml" 的人,我会在其中找到类似这样的内容:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\AmosH\Documents\Workspace\general\management\__init__.py'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at Microsoft.VisualStudio.Text.Implementation.TextDocumentFactoryService.OpenFileGuts(String filePath, DateTime& lastModifiedTimeUtc, Int64& fileSize) at Microsoft.VisualStudio.Text.Implementation.TextDocumentFactoryService.OpenFile(String filePath, DateTime& lastModifiedTimeUtc, Int64& fileSize) at Microsoft.VisualStudio.Text.Implementation.TextDocumentFactoryService.CreateAndLoadTextDocument(String filePath, IContentType contentType, Boolean attemptUtf8Detection, Boolean& characterSubstitutionsOccurred) at Microsoft.VisualStudio.Editor.Implementation.TextDocData.InitializeNewTextBufferFromFile(String fileName, Object source) at Microsoft.VisualStudio.Editor.Implementation.TextDocData.Load(String pszFilename, UInt32 grfMode, Int32 fReadOnly, Object source) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(Int32 hr, Int32[] expectedHRFailure) at Microsoft.VisualStudio.Shell.VsShellUtilities.OpenDocument(IServiceProvider provider, String fullPath, Guid logicalView, IVsUIHierarchy& hierarchy, UInt32& itemID, IVsWindowFrame& windowFrame, IVsTextView& view) at Microsoft.VisualStudioTools.Project.VsUtilities.OpenDocument(IServiceProvider serviceProvider, String filename, IVsTextView& viewAdapter, IVsWindowFrame& pWindowFrame) at Microsoft.VisualStudioTools.Project.VsUtilities.NavigateTo(IServiceProvider serviceProvider, String filename, Guid docViewGuidType, Int32 line, Int32 col) at Microsoft.PythonTools.Project.CustomCommand.ErrorListRedirector.OnNavigate(Object sender, EventArgs e) at Microsoft.VisualStudio.Shell.Task.OnNavigate(EventArgs e) at Microsoft.VisualStudio.Shell.Task.Microsoft.VisualStudio.Shell.Interop.IVsTaskItem.NavigateTo() at Microsoft.VisualStudio.ErrorListPkg.Shims.ErrorListPackageEventProcessor.PreprocessNavigate(ITableEntryHandle entryHandle, TableEntryNavigateEventArgs e) at Microsoft.VisualStudio.Shell.TableControl.Implementation.TableControlEventManager.<>c__DisplayClass40_0.<OnEntryElementNavigate>b__2(ITableControlEventProcessor p, ITableEntryHandle entry) at Microsoft.VisualStudio.Shell.TableControl.Implementation.TableControlEventManager.<>c__DisplayClass60_1`1.<EventProcessorHandler>b__0() at Microsoft.VisualStudio.Text.Utilities.GuardedOperations.CallExtensionPoint(Object errorSource, Action call) --- End of stack trace from previous location where exception was thrown --- at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

显然 Visual Studio 2017 中存在一个错误,如果您没有选择 .pyproj 文件的位置作为项目的根目录,就会导致此问题。在我的例子中,我选择了父目录,并且 Visual Studio 2017 没有做出 pylint 在这些情况下工作所需的适当调整。每当我 运行 pylint 到 Visual Studio 2017 时,它都会在父目录中寻找模块,即使我可以浏览我的项目并使用 Visual Studio 2017 毫无问题地编辑文件。它确实很奇怪。