How to run Python script in ipython?


I’m just getting my feet wet with Python v3.5.2. I’ve installed IPython via Anaconda and am now attempting to run a simple program.

I wrote a simple print("Hello World!") script in a text editor and saved it as “C:Python”.

I’ve attempted various ways to execute, with and without quotes, and I get one of two errors:

In [34]: %run C:Python 
ERROR: File `'C:Python/py'` not found.


In [35]: ipython 'C:Python' 
  File "<ipython-input-35-30b39bc825d7>", line 1
    ipython 'C:Python'

SyntaxError: invalid syntax

What am I doing wrong?

Asked By: RobertF



You don’t need to change the spaces in your path – as @MadPhysicist said, sometimes you don’t have control over that. Instead, you can surround your path in quotes:

In [42]: %run "C:Python"
Answered By: MattDMo

It seems backslash or double backslash will not work in windows, similarly when used double or single quotes, shows error like

not found

. Forward slash no quote path works, for example:

%run -i C:/Users/xxx.yyy/.ipython/profile_default/startup/

But need to change all magic command to function call first such as %matplotlib inline to get_ipython().run_line_magic('matplotlib', 'inline'), %load_ext autoreload to get_ipython().run_line_magic('load_ext', 'autoreload') and etc.

Answered By: Frank
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.