Skip to content Skip to sidebar Skip to footer

How To Execute Python Scripts With Python Command

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\Hel

Solution 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"

Solution 2:

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..

Solution 3:

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 osos.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.

Solution 4:

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")

Post a Comment for "How To Execute Python Scripts With Python Command"