0

I want to run the Python Script by using python command from IDLE Python GUI.
The location of the file is C:\Users\DELL\Desktop\Hello.py
When I run python C:\Users\DELL\Desktop\Hello.py with Windows command promt, it works. However, when I try with IDLE Python GUI and Python (command line), it does not work and gives me a message SyntaxError: invalid syntax
Capture

Chid0ry
  • 1
  • 1
  • 1
  • It isn't valid python syntax. If you want to call another script from within python, maybe you need `subprocess`. I'm not sure I understand what you're trying to do. – roganjosh May 12 '18 at 11:35
  • 1
    Possible duplicate of [What is the best way to call a Python script from another Python script?](https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-python-script-from-another-python-script) – Simon May 12 '18 at 11:43
  • Please put your error message as text in your question. See: [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – sɐunıɔןɐqɐp May 12 '18 at 11:43

4 Answers4

1

inside python shell, you can import the module. This way, you can see "Hello" printed on shell once u import it.

>>> import sys
>>> sys.path.append('C:\Users\DELL\Desktop')
>>> import Hello
"Hello"
Ali Yılmaz
  • 1,463
  • 1
  • 9
  • 23
0

When using IDLE, you are already "inside" python (in its REPL)..
You should "load" (import) the file instead..
See here https://stackoverflow.com/a/21650698/5121955 (exact same situation), where you can find many solutions with their advantages..

SanjiBukai
  • 535
  • 4
  • 15
0

You cannot do this from the Python interpreter, since this is not Python syntax, if you want to do this it has to be from command prompt or execute it as a system command:

import os
os.system('python  C:\\Users\\DELL\Desktop\\Hello.py')

Or use subprocess.call

If you want to run your python file from another Python file see How can I make one python file run another?

If you want to run it from the IDLE simply select open and navigate to the desired location and select run.

Simon
  • 8,992
  • 8
  • 49
  • 76
0

If it is executable script (as you stated, it works from command line with the python command), you should be able to execute it directly from python with the following statements

import os
os.system("C:\\Users\\DELL\\Desktop\\Hello.py")
CermakM
  • 994
  • 1
  • 10
  • 14