» pip install runfile
Why does 'runfile' work in Spyder but not in Powershell?
How to work with pure python file (.py)
hide the 'runfile (...)' expression when running a python script from Spyder
Run a Python file in Command Prompt
Videos
I have Anaconda3 installed with python 3.4, on Windows 8. In the interest of really trying to understand what's going on, I'm practicing all the different ways to run a script - using the 'Run file' button in Spyder, using the console in Spyder, command line within Powershell, and command line after starting python in Powershell.
I was curious as to why the 'runfile' command works in the Spyder console but not in python within Powershell. That is, I can put this in in the Spyder console:
In [1]: runfile('endome\pythonscript.py')but, when I try to do the same thing after launching python in Powershell, runfile isn't recognized:
>>> runfile('endome\pythonscript.py')
Traceback (most recent call last):
File "(stdin)", line 1, in (module)
NameError: name 'runfile' is not definedNow, I did learn how to run a script in Powershell using:
>>> exec(open('endome\pythonscript.py').read())But, I'm just curious why runfile works in Spyder but not in Powershell (or cmd for that matter).
Option 1: Call the interpreter
- For Python 2:
python <filename>.py - For Python 3:
python3 <filename>.py
Option 2: Let the script call the interpreter
- Make sure the first line of your file has
#!/usr/bin/env python. - Make it executable -
chmod +x <filename>.py. - And run it as
./<filename>.py
Just prefix the script's filename with python. E.g.:
python filename.py