1

This command runs under bash on Linux:

python file.py variables

But when I write it to the IPython console in Spyder I get:

SyntaxError: invalid syntax

Q: How can I run a python script using the IPython console in Spyder?

Ente
  • 2,301
  • 1
  • 16
  • 34
gorte
  • 23
  • 1
  • 6

2 Answers2

2

(Spyder maintainer here) To run a Python file in Spyder, you just need to open it in its editor and the go to the menu

Run > Run file

or press F5. That basically reads the contents of the file and executes it with exec (as it was suggested in the answer by Jeremy Hue).

If you want to pass arguments to your script, please see my answer for that here.

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
0

Your IPython console is already running Python, whereas the command python file.py in bash is basically saying 'run file.py using Python'.

Check out this solution if you want to run file.py explicitly via the IPython console run program in Python shell

Jeremy H
  • 408
  • 2
  • 11
  • Hi thanks. The exec(open("C:\\test.py").read()) command is working good. But do you know how to add paramters/options when calling test.py? – gorte Oct 29 '19 at 22:40
  • I have this: python rsatool.py -f PEM -o key.pem -n 13826123222358393307 -d 9793706120266356337. How could I add all the paramters to the call? Thx – gorte Oct 29 '19 at 22:46
  • You will then have to change your approach. Have a look here https://stackoverflow.com/questions/5788891/execute-a-file-with-arguments-in-python-shell – Jeremy H Oct 29 '19 at 23:42