如何运行 Python 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12873542/
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
How to run a Python project?
提问by Jamol
I have installed the Enthought Python distribution on my computer, but I don't have any idea how to use it. I have PyLab and IDLE but I want to run .py files by typing the following command:
我已经在我的电脑上安装了 Enthought Python 发行版,但我不知道如何使用它。我有 PyLab 和 IDLE,但我想通过键入以下命令来运行 .py 文件:
python fileName.py
蟒蛇文件名.py
I don't know where to write this command: IDLE, PyLab or Python.exe or Windows command prompt. When I do this in IDLE it says:
我不知道在哪里写这个命令:IDLE、PyLab 或 Python.exe 或 Windows 命令提示符。当我在 IDLE 中执行此操作时,它说:
SyntaxError: invalid syntax
语法错误:无效语法
Please help me to figure this out.
请帮我解决这个问题。
采纳答案by Ionut Hulub
- Open a command prompt: Press ? Win and Rat the same time, then type in
cmdand press ? Enter - Navigate to the folder where you have the ".py" file (use
cd ..to go one folder back orcd folderNameto enterfolderName) - Then type in
python filename.py
- 打开命令提示符:同时按? Win 和R,然后输入
cmd并按? Enter - 导航到您拥有“.py”文件的文件夹(用于
cd ..返回一个文件夹或cd folderName进入folderName) - 然后输入
python filename.py
回答by Difusio
Indeed, the command to run a Python file should be run in the command prompt. Python should be in your path variable for it to work flexible.
实际上,运行 Python 文件的命令应该在命令提示符下运行。Python 应该在您的路径变量中,以便它能够灵活地工作。
When the python folder is added to path you can call python everywhere in the command prompt, otherwise just in your python install folder.
将 python 文件夹添加到路径后,您可以在命令提示符中的任何地方调用 python,否则只能在您的 python 安装文件夹中调用。
The following is from the python website:
以下内容来自python网站:
Windows has a built-in dialog for changing environment variables (following guide applies to XP classical view): Right-click the icon for your machine (usually located on your Desktop and called “My Computer”) and choose Properties there. Then, open the Advanced tab and click the Environment Variables button.
In short, your path is:
My Computer ? Properties ? Advanced ? Environment Variables In this dialog, you can add or modify User and System variables. To change System variables, you need non-restricted access to your machine (i.e. Administrator rights).
Another way of adding variables to your environment is using the set command in a command prompt:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
Windows 有一个用于更改环境变量的内置对话框(以下指南适用于 XP 经典视图):右键单击您机器的图标(通常位于您的桌面上并称为“我的电脑”),然后在那里选择属性。然后,打开高级选项卡并单击环境变量按钮。
简而言之,您的路径是:
我的电脑 ?特性 ?先进的 ?环境变量 在此对话框中,您可以添加或修改用户和系统变量。要更改系统变量,您需要不受限制地访问您的机器(即管理员权限)。
另一种向环境添加变量的方法是在命令提示符中使用 set 命令:
设置 PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
If you do it via My Computer, then look for the line named path in Enviroment Variables. Give that the value of your Python installation folder.
如果您通过我的电脑执行此操作,请在环境变量中查找名为 path 的行。赋予您的 Python 安装文件夹的值。


