在 PowerShell 中运行 Python?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19676403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:20:54  来源:igfitidea点击:

Running Python in PowerShell?

pythonpowershell

提问by Scherf

I am attempting to learn the very basics of Python using the guide "Learn Python the hard way"by Zed A. Shaw. The problem that I am having is that I can run Python scripts, but only when using .\in front of the name. This opens up CMD for a split second and then closes.

我正在尝试使用Zed A. Shaw 的“Learn Python the hard way”指南来学习 Python 的基础知识。我遇到的问题是我可以运行 Python 脚本,但只能在.\名称前使用。这会打开 CMD 一瞬间然后关闭。

If I attempt to run the file it returns that the file is not an operable program file, script, etc..

如果我尝试运行该文件,它会返回该文件不是可操作的程序文件、脚本等。

I've found multiple questions on Stack Overflow that relate to this question, but none of the solutions have worked for me.

我在 Stack Overflow 上发现了多个与此问题相关的问题,但没有一个解决方案对我有用。

Two things I've tried:

我尝试过的两件事:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")

and

$env:PATH =$env:PATH+";."

Source: (How do you remove the PowerShell requirement that scripts and executables be preceded by ".\"?)

来源:(您如何删除脚本和可执行文件前面带有“.\”的 PowerShell 要求?

When I check the environment variable PATH it has the correct path within it, so what other things could be causing this?

当我检查环境变量 PATH 时,它包含正确的路径,那么还有什么其他原因可能导致这种情况?

采纳答案by Sukrit Kalra

Since, you are able to run Python in PowerShell. You can just do python <scriptName>.pyto run the script. So, for a script named test.pycontaining

因为,您可以在 PowerShell 中运行 Python。您只需python <scriptName>.py运行脚本即可。所以,对于一个名为test.py包含的脚本

name = raw_input("Enter your name: ")
print "Hello, " + name

The PowerShell session would be

PowerShell 会话将是

PS C:\Python27> python test.py
Enter your name: Monty Python
Hello, Monty Python
PS C:\Python27>

回答by ajknzhol

As far as I have understood your question, you have listed two issues.

据我了解你的问题,你列出了两个问题。

PROBLEM 1:

问题 1:

You are not able to execute the Python scripts by double clicking the Python file in Windows.

您无法通过在 Windows 中双击 Python 文件来执行 Python 脚本。

REASON:

原因:

The script runs too fast to be seen by the human eye.

脚本运行速度太快,人眼无法看到。

SOLUTION:

解决方案:

Add input()in the bottom of your script and then try executing it with double click. Now the cmd will be open until you close it.

input()在脚本底部添加,然后尝试双击执行它。现在 cmd 将一直打开,直到您关闭它。

EXAMPLE:

例子:

print("Hello World")
input()


PROBLEM 2:

问题 2:

./issue

./问题

SOLUTION:

解决方案:

Use Tabto autocompletethe filenames rather than manually typing the filename with ./autocomplete automatically fills all this for you.

使用Tab键自动完成文件名,而不是手动键入的文件名./自动完成自动填充这一切都为您服务。

USAGE:

用法:

CDinto the directory in which .pyfiles are present and then assume the filename is test.pythen type python teand then press Tab, it will be automatically converted to python ./test.py.

CD进入.py文件所在的目录,然后假设文件名是test.py然后输入python te然后按Tab键,它会自动转换为python ./test.py.

回答by David Douglas

Go to Control PanelSystem and SecuritySystem, and then click Advanced system settingson the left hand side menu.

转到控制面板系统和安全系统,然后单击左侧菜单中的高级系统设置

On the Advancedtab, click Environment Variables.

高级选项卡上,单击环境变量

Under 'User variables' append the PATH variable with path to your Python install directory:

在“用户变量”下,将带有路径的 PATH 变量附加到 Python 安装目录:

C:\Python27;

回答by David Douglas

The command [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User") is not a Python command. Instead, this is an operating system command to the set the PATH variable.

命令 [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User") 不是 Python 命令。相反,这是一个用于设置 PATH 变量的操作系统命令。

You are getting this error as you are inside the Python interpreter which was triggered by the command pythonyou have entered in the terminal (Windows PowerShell).

您在 Python 解释器中遇到此错误,这是由python您在终端 (Windows PowerShell) 中输入的命令触发的。

Please note the >>>at the left side of the line. It states that you are on inside Python interpreter.

请注意>>>该行左侧的 。它表明您在 Python 解释器内部。

Please enter quit() to exit the Python interpreter and then type the command. It should work!

请输入 quit() 退出 Python 解释器,然后输入命令。它应该工作!

回答by Mustafa jahjoum

The default execution policy, "Restricted", prevents all scripts from running, including scripts that you write on the local computer.

默认执行策略“受限”会阻止所有脚本运行,包括您在本地计算机上编写的脚本。

The execution policy is saved in the registry, so you need to change it only once on each computer.

执行策略保存在注册表中,因此您只需在每台计算机上更改一次。

To change the execution policy, use the following procedure:

要更改执行策略,请使用以下过程:

  1. Start Windows PowerShell with the "Run as administrator" option.

  2. At the command prompt, type:

    Set-ExecutionPolicy AllSigned

    -or-

    Set-ExecutionPolicy RemoteSigned

  1. 使用“以管理员身份运行”选项启动 Windows PowerShell。

  2. 在命令提示符下,键入:

    Set-ExecutionPolicy AllSigned

    -或者-

    Set-ExecutionPolicy RemoteSigned

The change is effective immediately.

更改立即生效。

To run a script, type the full name and the full path to the script file.

要运行脚本,请键入脚本文件的全名和完整路径。

For example, to run the Get-ServiceLog.ps1script in the C:\Scriptsdirectory, type:

例如,要Get-ServiceLog.ps1C:\Scripts目录中运行脚本,请键入:

C:\Scripts\Get-ServiceLog.ps1

And to the Python file, you have two points. Try to add your Python folder to your PATH and the extension .py.

对于 Python 文件,您有两点。尝试将您的 Python 文件夹添加到您的 PATH 和扩展名.py.

To PATHEXT from go properties of computer. Then click on advanced system protection. Then environment variable. Here you will find the two points.

从计算机的 go 属性到 PATHEXT。然后点击高级系统保护。然后是环境变量。在这里你会发现两点。

回答by Lucas Coelho

Using CMD you can run your python scripts as long as the installed python is added to the path with the following line:

使用 CMD,你可以运行你的 python 脚本,只要安装的 python 使用以下行添加到路径中:

C: \ Python27;

The (27) is example referring to version 2.7, add as per your version.

(27) 是指版本 2.7 的示例,根据您的版本添加。

Path to system path:

系统路径的路径:

Control Panel => System and Security => System => Advanced Settings => Advanced => Environment Variables.

控制面板 => 系统和安全 => 系统 => 高级设置 => 高级 => 环境变量。

Under "User Variables," append the PATH variable to the path of the Python installation directory (As above).

在“用户变量”下,将 PATH 变量附加到 Python 安装目录的路径(如上)。

Once this is done, you can open a CMD where your scripts are saved, or manually navigate through the CMD.

完成此操作后,您可以打开保存脚本的 CMD,或手动浏览 CMD。

To run the script enter:

要运行脚本,请输入:

C: \ User \ X \ MyScripts> python ScriptName.py

回答by Whitney Kretz

Go to Python Website/dowloads/windows. Download Windows x86-64 embeddable zip file. 2. Open Windows Explorer

转到 Python 网站/下载/窗口。下载 Windows x86-64 可嵌入 zip 文件。2. 打开 Windows 资源管理器

open zipped folder python-3.7.0 In the windows toolbar with the Red flair saying “Compressed Folder Tool” Press “Extract” button on the tool bar with “File” “Home “Share” “View” Select Extract all Extraction process is not covered yet Once extracted save onto SDD or fastest memory device. Not usb. HDD is fine. SDD Users/butte/ProgramFiles blah blah ooooor D:\Python Or Hook up to your cloud 3. Click your User Icon in the Windows tool bar.

打开压缩文件夹python-3.7.0 在windows工具栏中用红色天赋说“压缩文件夹工具”按工具栏上的“提取”按钮“文件”“主页”“共享”“查看”选择提取所有提取过程不是尚未覆盖 一旦提取保存到 SDD 或最快的存储设备。不是usb。硬盘没问题。SDD Users/butte/ProgramFiles blah blah ooooor D:\Python 或连接到您的云 3. 单击 Windows 工具栏中的用户图标。

Search environment variable Proceed with progressing with “Environment Variables” button press Under the “user variables” table select “New..” After the Canvas of Information Add Python in Variable Name Select the “D:\Python\python-3.7.0-embed-amd64\python.exe;” click ok Under the “System Variables” label and in the Canvas the first row has a value marked “Path” Select “Edit” when “Path” is highlighted. Select “New” Enter D:\Python\python-3.7.0-embed-amd click ok Ok Save and double check Open Power Shell python --help

搜索环境变量继续进行“环境变量”按钮按下在“用户变量”表下选择“新建..”在信息画布后在变量名称中添加Python 选择“D:\Python\python-3.7.0-嵌入amd64\python.exe;” 单击确定在“系统变量”标签下,在画布中,第一行有一个标记为“路径”的值,当“路径”突出显示时选择“编辑”。选择“新建”输入 D:\Python\python-3.7.0-embed-amd 点击确定 Ok 保存并仔细检查 Open Power Shell python --help

python --version

蟒蛇--版本

Source to tutorial https://thedishbunnybitch.com/2018/08/11/installing-python-on-windows-10-for-powershell/

教程来源 https://thedishbunnybitch.com/2018/08/11/installing-python-on-windows-10-for-powershell/