Why is Python broken in Visual Studio 2017?

为君一笑 提交于 2019-12-24 12:17:28

问题


Before installing Visual Studio 2017, all my python files were successfully running using py (or py3) in PowerShell, Git Bash, and Cygwin Terminal, but not CMD.exe or MSYS:

py myscript.py

This executable was installed directly by the Python 3.7 installer, and I configured the path variables for it.

After installing VS 2017, Microsoft inserted the path to their VS-specific copy of py.exe, C:\Windows\py.exe into the system PATH variable. This copy of python does nothing when run directly, and will not respond to exit() command to terminate its thread, and scripts do nothing when passed into it. Literally nothing works, not even print() statements.

In Visual Studio, after opening the "Solution Explorer" window and right clicking on script.py and clicking Start without Debugging, nothing happens.

If I click Start with Debugging, then it changes the view to a debugging mode, and "runs" the program (the pause and stop buttons are showing), but no execution window shows up. The Output panel automatically selects "Debug" and there is no output to see there. The "autos" and "locals" panels are not populated, indicating that nothing is actually running.

I don't want a work-around. I know I could delete this entry from PATH, but the official Python installation may not be compatible with VS 2017 or might lack integrations. I want to know why Microsoft's copy of py.exe fails at everything and what their official fix is for this problem.


回答1:


This isn't really a proper answer, because I'm not sure about how it relates to the problem you're having with respect to Visual Studio 2017 and PowerShell—but since you asked me to post it as though it was one, I'm doing so.


Microsoft didn't install the C:\Windows\py.exe executable, the Python installer did.

Also note, it's not a Python interpreter, it's a command-line "launcher" utility that looks for special shebang (#! prefixed) comments at the beginning of script files. It makes running Python scripts on Windows work somewhat like it does on Linux—and helps when you have more than one Python version (such as both 2.x and 3.x) installed on your system.

See the following about using Python on Windows in the Python documentation. Also see Vinay Sajip's (the author) version control repository for the pylauncher project which is where it originated.

As I said in the preface, I'm not sure how all this relates to your problem, but feel that understanding how things normally work may help you resolve the issue.

Hope you found it insightful...




回答2:


Visual Studio 2017 uses PowerShell as its shell / execution environment.

The problem is due to some issue in PowerShell adopting the properties of Cygwin's execution environment including the virtual path.

The problem randomly went away. One second, PowerShell is reporting its working directory as /cygdrive/c/folder/to/working/directory and acting exactly like Cygwin Terminal. The next time I ran PowerShell, it functioned correctly, returning this kind of output to pwd:

PS C:\WINDOWS\system32> pwd

Path
---- 
C:\WINDOWS\system32

This problem is a combination of two problems. The first one has to do with py.exe not recognizing Cygwin as an interactive shell, as explained in this answer.

The second has to do with PowerShell adopting Cygwin's execution environment. Not sure how this happens, or why it suddenly stopped happening.



来源:https://stackoverflow.com/questions/52617010/why-is-python-broken-in-visual-studio-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!