vs2017下发现解决python运行出现‘No module named "XXX""的解决办法

   对于使用vs2017开发python程序无疑发现,在解决方案资源管理器中设置把两个xxx.py,yyy.py文件都设置为启动文件,然后分别在vs2017这个IDE下运行这个两个文件在项目工程中运行,发现其中一个文件yyy.py不能正常运行,,同时提示“No module named "XXXyyy""的错误提示,但是在vs2017之外的控制台模式下比如python yyy.py却能正常运行,其它xxx.py在IDE内外均能正常执行。

这让我折腾了不少时间,上网搜索解决办法,五法八门,还能不能得正确的解决办法,后来自己冷静下来想了,是不是vs2017环境本身的问题,首先想到是该工程配置文件是不是写死了启动文件,使用文本编辑器打开该配置文件,搜了能运行的xxxx.py的文件名,果然在那里面(详见下面python工程配置文件),然后尝试把解决方案资源管理器中设置为启动文件也不能运行的文件名yyy.py全部替换xxx.py,然后重新加载工程,我的乖乖,果然成功了.

 对比一下pycharm环境下,在这里只要选择相应的运行文件均为正常执行,看来pycharm和vs2017在python文件运行的建构上明显不一样,pycharm还是比较专业吧,毕竟vs2017后来在开始支持python开发,还不够完美,需要不断的完善。

python工程配置文件如下:(比如pythonApplication1.pyproj)

 

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>55a475f5-dc5d-489c-94e7-d1408984e407</ProjectGuid>
<ProjectHome>.</ProjectHome>
<StartupFile>xxx.py</StartupFile>
<SearchPath>........pythonenvspy37Libsite-packages</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<Name>PythonApplication1</Name>
<RootNamespace>PythonApplication1</RootNamespace>
<InterpreterId>MSBuild|py37|$(MSBuildProjectFullPath)</InterpreterId>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="xxx.py" />
</ItemGroup>
<ItemGroup>
<Interpreter Include="........pythonenvspy37">
<Id>py37</Id>
<Version>3.7</Version>
<Description>py37 (Python 3.7 (64-bit))</Description>
<InterpreterPath>Scriptspython.exe</InterpreterPath>
<WindowsInterpreterPath>Scriptspythonw.exe</WindowsInterpreterPath>
<PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable>
<Architecture>X64</Architecture>
</Interpreter>
</ItemGroup>
<ItemGroup>
<Content Include="requirements.txt" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)MicrosoftVisualStudiov$(VisualStudioVersion)Python ToolsMicrosoft.PythonTools.targets" />
<!-- Uncomment the CoreCompile target to enable the Build command in
Visual Studio and specify your pre- and post-build commands in
the BeforeBuild and AfterBuild targets below. -->
<!--<Target Name="CoreCompile" />-->
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
</Project>

原文地址:https://www.cnblogs.com/it-tsz/p/10545129.html