How to run a Python project?
Asked Answered
C

3

5

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:

python fileName.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:

SyntaxError: invalid syntax

Please help me to figure this out.

Cuyp answered 13/10, 2012 at 13:26 Comment(2)
You need to do that in Command Prompt. From the directory where you saved your fileName.py file. IDLE is used to write code.Heidy
Read the python on windows FAQ docs.python.org/faq/windows.htmlAstomatous
P
4
  • Open a command prompt: Press ⊞ Win and R at the same time, then type in cmd and press ↵ Enter
  • Navigate to the folder where you have the ".py" file (use cd .. to go one folder back or cd folderName to enter folderName)
  • Then type in python filename.py
Pepper answered 13/10, 2012 at 13:27 Comment(3)
of course. but python usually does that for you when you install it.Pepper
Wasn't aware it modified path settings... docs.python.org/using/windows.htmlJuryrigged
Since this is quite a fundamental question and this is the accepted answer I'd suggest adding some info on the path matter, assuming it's set doesn't help a beginning Pythonista.Americaamerican
A
3

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.

When the python folder is added to path you can call python everywhere in the command prompt, otherwise just in your python install folder.

The following is from the python website:

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

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.

Americaamerican answered 13/10, 2012 at 13:32 Comment(0)
T
0

Set PYTHON variable to point to the full path of python.exe.

enter image description here

Then type in command prompt console window:

C:\path_to_folder\ python filename.py

Taritariff answered 30/5, 2018 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.