[英]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: *到目前为止我做了什么: *
C:\Users\enriq\AppData\Local\Programs\Python\Python39\
C:\用户\enriq\AppData\Local\Programs\Python\Python39\
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 输入“帮助”,“版权” ”、“学分”或“许可证”以获取更多信息
C:\Users\enriq>Desktop>MyScripts>intro.py
'Desktop' is not recognized as an internal or external command, operable program or batch file.
'Desktop' 不是内部或外部命令,也不是可运行的程序或批处理文件。
%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\Python39C:\Users\enriq\AppData\Local\Programs\Python\Python39
C:\Users\enriq\OneDrive\Desktop\MyScriptsC:\用户\enriq\OneDrive\桌面\MyScripts
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。
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 打开脚本?
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.