简体   繁体   English

如何让 Python 脚本在 Windows 10 上打开并执行?

[英]How can I get Python scripts to open and execute on Windows 10?

The Situation:情况:

I am trying to install and set-up Python 3.9.7 on Windows 10 Pro.我正在尝试在 Windows 10 Pro 上安装和设置 Python 3.9.7。 However after install I am unable to open any Python Scripts from the Command Prompt, Visual Studio Code, or PyCharm.但是,安装后我无法从命令提示符、Visual Studio 代码或 PyCharm 打开任何 Python 脚本。

What I have done so far: *到目前为止我做了什么: *

  1. Downloaded Python, adding to Path during installation and also removed the Path maximum lenght limit.下载 Python,在安装过程中添加到 Path 并删除了 Path 最大长度限制。 Python is installed in: Python 安装在:

C:\Users\enriq\AppData\Local\Programs\Python\Python39\ C:\用户\enriq\AppData\Local\Programs\Python\Python39\

  1. Python is recognized and executed from command promp as follows: Python 从命令提示符识别和执行如下:
C:\Users\enriq>python 

Python 3.9.7 (tags/vPython 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information Python 3.9.7 (tags/vPython 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32 输入“帮助”,“版权” ”、“学分”或“许可证”以获取更多信息

  1. Tried to open an IDLE executable in the command prompt:试图在命令提示符下打开 IDLE 可执行文件:
C:\Users\enriq>Desktop>MyScripts>intro.py

'Desktop' is not recognized as an internal or external command, operable program or batch file. 'Desktop' 不是内部或外部命令,也不是可运行的程序或批处理文件。

  1. In Environment Variables:在环境变量中:
  • Added at the end of System Path the User Path.在系统路径末尾添加用户路径。
  • Added at the beginning of User Path, the System Path.在用户路径、系统路径的开头添加。 Both paths are identical now;现在两条路径是相同的;

%SystemRoot%\system32 %SystemRoot% %SystemRoot%\System32\Wbem %SYSTEMROOT%\System32\WindowsPowerShell\v1.0 %SystemRoot%\system32 %SystemRoot% %SystemRoot%\System32\Wbem %SYSTEMROOT%\System32\WindowsPowerShell\v1.0
%SYSTEMROOT%\System32\OpenSSH %SYSTEMROOT%\System32\OpenSSH
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps C:\Users\enriq\AppData\Local\Programs\Python\Python39\Scripts %USERPROFILE%\AppData\Local\Microsoft\WindowsApps C:\Users\enriq\AppData\Local\Programs\Python\Python39\Scripts
C:\Users\enriq\AppData\Local\Programs\Python\Python39 C:\Users\enriq\AppData\Local\Programs\Python\Python39
C:\Users\enriq\OneDrive\Desktop\MyScripts C:\用户\enriq\OneDrive\桌面\MyScripts

  1. In Registry Editor:在注册表编辑器中:

Computer\HKEY_CURRENT_USER\Environment, set the type of the User Variable to REG_EXPAND_SZ. Computer\HKEY_CURRENT_USER\Environment,设置User Variable类型为REG_EXPAND_SZ。

  1. Clicked OK after every change, checked there are no spaces after every line in the environment variables.每次更改后单击确定,检查环境变量中每行后没有空格。 I tried to run the script after every change.我尝试在每次更改后运行脚本。

Additional Information:附加信息:

My machine is:我的机器是:

HP laptop recently formatted: processor Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz 2.90 GHz, system type: 64-bit operating system, x64-based processor. HP 笔记本电脑最近格式化:处理器 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz 2.90 GHz,系统类型:64 位操作系统,基于 x64 的处理器。

The Question:问题:

How could I resolve this and get Python to open the scripts?我怎样才能解决这个问题并让 Python 打开脚本?

在此处输入图像描述

This is really a question than an answer but have you associated.py with Python?这实际上是一个问题,而不是一个答案,但是您是否将 .py 与 Python 相关联? You can do it this way.你可以这样做。

When you say you're trying to open a python script, do you mean you want to run the script, or you want to edit it?当您说您正在尝试打开python 脚本时,您的意思是要运行该脚本,还是要编辑它? In either case, step 3 is where things are going wrong.无论哪种情况,第 3 步都是出错的地方。 To run your script, change your command to something like:要运行脚本,请将命令更改为:

python C:\Users\enriq\Desktop\MyScripts\intro.py

If you want to inspect or edit the source code, you should be able to do that via IDLE's graphical user interface.如果你想检查或编辑源代码,你应该能够通过 IDLE 的图形用户界面来完成。 If you already have IDLE open, look for File > Open in the menu bar and browse to the location of the file you want to edit.如果您已经打开了 IDLE,请在菜单栏中查找“ File > Open ”,然后浏览到您要编辑的文件所在的位置。

Outside of using IDLE's user interface to open the file, you could also browse to the intro.py file icon in the MyScripts folder on your desktop and click to open it.除了使用 IDLE 的用户界面打开文件外,您还可以浏览到桌面上MyScripts文件夹中的intro.py文件图标,然后单击打开它。 Windows will open the file using whatever application is mapped to .py files, and if there is no existing mapping, it should ask you which application you want to use to open the file. Windows 将使用映射到.py文件的任何应用程序打开文件,如果没有现有映射,它应该询问您要使用哪个应用程序打开文件。 You can then choose IDLE, or whichever other editor you like.然后您可以选择 IDLE,或您喜欢的任何其他编辑器。

To execute.执行。 py files you have to invoke python.exe like this: python file.py or you can execute code with the - c switch: python -c "print('hi')" To execute installed modules use the -m switch: python -m pip I remember that, in the Python installer for Windows, there's also the option to associate. py 文件,你必须像这样调用 python.exe: python file.py或者你可以使用 - c 开关执行代码: python -c "print('hi')"要执行已安装的模块,请使用 -m 开关: python -m pip我记得,在 Windows 的 Python 安装程序中,还有关联的选项。 py extension with Python (but personally I don't see the need, since most of the time I use an IDE). py 扩展名为 Python(但我个人认为没有必要,因为大部分时间我都使用 IDE)。 Official docs in case you haven't read them: https://docs.python.org/3/using/windows.html如果您还没有阅读官方文档: https://docs.python.org/3/using/windows.html

Try this code on you terminal C:\Users\enriq>Desktop>MyScripts>python intro.py or this C:\Users\enriq>Desktop>MyScripts>py intro.py在终端C:\Users\enriq>Desktop>MyScripts>python intro.py或此C:\Users\enriq>Desktop>MyScripts>py intro.py上尝试此代码

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

相关问题 如何使python 3脚本从Windows 10上的.bat文件执行? - How to make python 3 scripts execute from .bat file on Windows 10? 如何使用 python 脚本打开 Windows 10 应用程序? - How can I open a Windows 10 app with a python script? 如何在 Windows 中执行 Python 脚本? - How to execute Python scripts in Windows? 如何从另一个目录执行python脚本 - How can I execute python scripts from another directory 如何在 WINdows 10 中打开 Python IDLE (Shell WIndow)? - How do I open Python IDLE (Shell WIndow) in WIndows 10? 如何在 Windows 10 上打开和查看 Python PY 文件? - How do I open and view Python PY files on Windows 10? 如何在 Windows 10 中使用 python3.9 检查文件是否在我的 python 脚本之外的另一个进程中主动打开? - How can I check if a file is actively open in another process outside of my python script with python3.9 in Windows 10? 如何使用 python 获取 windows 10 中当前正在播放的媒体的标题 - How can I get the title of the currently playing media in windows 10 with python 如何让Spyder直接从Windows资源管理器打开python脚本(.py文件) - How to get Spyder to open python scripts (.py files) directly from Windows Explorer 如何在 IIS windows 10 上使用 python 3.x 脚本 - How to use python 3.x scripts on IIS windows 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM