» 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
I have been at this for an hour. I wrote the script, went line by line in interactive mode (without opening the file) and it worked perfectly. Now I'm trying to actually run the file from the python (>>>) interpreter so that I can interact with it directly. I'm in the right directory. I'm using both Powershell and Kali Linux. I was trying first with powershell, but since the course is Linux based, I switched to my Kali Linux VM. Same.
The course says "Hint: Use -i when executing the file in the terminal"
I can execute it from the terminal, but not interactively in the python interpreter.
Please have mercy on me. I've looked everywhere and exec() doesn't work.
exec(report.py)
NameError : name 'report' is not defined
If it matters, the code defines a function that uses csv to enter data into a list of tuples. The function takes one argument : a csv file. I did all the sololearn courses in python and am now working through https://github.com/dabeaz-course/practical-python .
I also took python for biologists, which was too easy. But it taught me to be proficient in using vim, and that's what I used to create the file.