简体   繁体   English

没有 PowerShell 的 Visual Studio 代码中的 Python

[英]Python in Visual Studio Code without PowerShell

So I need to use Python for my work.所以我的工作需要使用 Python。 Unfortunately, since recent hacks, the companies security policies are very strict and there is no way I'm getting admin rights.不幸的是,由于最近的黑客攻击,公司的安全政策非常严格,我无法获得管理员权限。 I managed to persuade our IT to install Python, Visual Studio Code, and the Python extension for it on my computer.我设法说服我们的 IT 在我的计算机上安装 Python、Visual Studio Code 和 Python 扩展。

If I try to run python commands in the Python interpreter it works.如果我尝试在 Python 解释器中运行 python 命令,它就可以工作。 But when I try to run a Python script in Visual Studio Code it hast to run Power Shell which is also blocked for security reasons.但是,当我尝试在 Visual Studio Code 中运行 Python 脚本时,它必须运行 Power Shell,出于安全原因,它也被阻止。 I get the following error:我收到以下错误:

The terminal process command 'C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe' failed to launch (exit code: {2})终端进程命令“C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe”启动失败(退出代码:{2})

Is there some way around this?有没有办法解决这个问题? To use Python in Visual Studio Code despite all of the security restrictions?尽管有所有安全限制,但要在 Visual Studio Code 中使用 Python?

I tried asking our IT department but they have no idea how to help me...我尝试询问我们的 IT 部门,但他们不知道如何帮助我...

Thank you in advance.先感谢您。

You can change the VS Code settings and tell it to not use PowerShell as shell in the integrated terminal.您可以更改 VS Code 设置并告诉它不要在集成终端中使用 PowerShell 作为 shell。

For example, I have put Windows Terminal in there, but you can also use CMD or any other shell such a git bash as long as it's available in your system. For example, I have put Windows Terminal in there, but you can also use CMD or any other shell such a git bash as long as it's available in your system.

Press ctrl+shift+p and type/select Open Settings (JSON) .ctrl+shift+p并输入/选择Open Settings (JSON) Then add some of the following to this configuration file, and save.然后将以下一些内容添加到此配置文件中,并保存。

For Windows Terminal:对于 Windows 端子:

"terminal.external.windowsExec": "C:\\Users\\<your-username>\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"

For CMD:对于 CMD:

"terminal.external.windowsExec": "C:\\Windows\\system32\\cmd.exe"

it's simple.这很简单。 You can do this, don't worry.你可以做到这一点,别担心。

You can get your answer from Visual Studio Code that how to execute the python code?.您可以从 Visual Studio Code 中获得如何执行 python 代码的答案。 here is the link ** https://stackoverflow.com/questions/29987840/how-to-execute-python-code-from-within-visual-studio-**code#:~:text=How%20to%20execute%20Python%20code%20from%20within%20Visual%20Studio%20Code这是链接** https://stackoverflow.com/questions/29987840/how-to-execute-python-code-from-within-visual-studio-**code#:~:text=How%20to%20execute %20Python%20code%20from%20within%20Visual%20Studio%20Code

This is a simple way, and you don't need any configuration:这是一种简单的方法,您不需要任何配置:

1.Install the Code Runner Extension ( https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner ) OR you can simply follow this path: Open VS code editer → go to extension (for opening extension press Ctrl + shift + x simulteneously) → search CODE RUNNER → install it. 1.安装代码运行器扩展( https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner或者你可以简单地按照这个路径:打开VS代码编辑器→go到扩展(打开扩展按Ctrl + shift + x同时) → 搜索 CODE RUNNER → 安装它。

2.Open the Python code file in Text Editor, and then use shortcut Ctrl + Alt + N , or press F1 and then select/type Run Code. 2.在文本编辑器中打开 Python 代码文件,然后使用快捷键Ctrl + Alt + N ,或按 F1 然后选择/键入运行代码。 The code will run and the output will be shown in the Output Window.代码将运行,output 将显示在 Output Window 中。

Hey friend,If you want to add the Python path, you could go to menu File → Preference → Settings, and add the Python path like below:嘿朋友,如果你想添加 Python 路径,你可以 go 到菜单文件 → 首选项 → 设置,然后添加 Python 路径如下:

"code-runner.executorMap": 

**{ "python": "\"C:\\Program Files\\Python35\\python.exe\"" }**

try it...试试看...

OR或者

TRY THIS,尝试这个,

I think you should have to install python https://www.python.org/downloads/.我认为您应该必须安装 python https://www.python.org/downloads/。

After installing.exe file of python.安装 python 的 .exe 文件后。 Go to downloads where you have downloaded the.exe file, open it by double click on it. Go 下载到您已下载 .exe 文件的位置,双击打开它。

enter image description here在此处输入图像描述

it looks like above window.它看起来像上面的 window。 before click on INSTALL NOW click on **Add python 3.9.6 to PATH.**then install it.在点击现在安装之前点击**将 python 3.9.6 添加到 PATH。** 然后安装它。

Now, go to VS code and try your program.现在,将 go 转换为 VS 代码并尝试您的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM