简体   繁体   English

如何在 IDLE 中运行与 Python 安装位置不同的文件夹中的 .py 文件?

[英]How to run a .py file in IDLE which is in a different folder other than where Python is installed?

I am trying to run a.py script using IDLE which is in a different folder say D:\Python\Practice whereas my python is installed in C:\Python.我正在尝试使用位于不同文件夹中的 IDLE 运行 a.py 脚本,例如 D:\Python\Practice,而我的 python 安装在 C:\Python 中。

I changed the directory to D:\Python\Practice where the script was present through os.chdir command.我通过 os.chdir 命令将目录更改为 D:\Python\Practice 脚本所在的位置。 Then I executed python xyz.py but it is throwing error.然后我执行了 python xyz.py 但它抛出错误。

import os
os.chdir("D:\Python\Practice")
python xyz.py

Syntax Error: Invalid Syntax语法错误:无效的语法

Using Windows CLI, you can navigate to your folder and execute the script as following:使用 Windows CLI,您可以导航到您的文件夹并执行以下脚本:

\> D:

D:\>cd D:\Python\Practice

D:\Python\Practice\>python xyz.py

Or simply give the full path of your file in the python command wherever you are:或者只需在 python 命令中提供文件的完整路径,无论您身在何处:

>python D:\Python\Practice\xyz.py

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

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