1

I am a python newbie, and I've written so many lines of code in a python script. I initially started copying and pasting each line in the iPython console and I feel like it is taking forever. Is there a more efficient way to do this?

Lets say I have a script called "movie_analysis.py" saved in my current working directory. How can I ask the program to read in the file, and then execute every line in the script one after the order (i.e in the order they were written).

Thanks in advance!!!

1
  • 3
    Do you mean something like import movie_analysis? Commented Apr 11, 2014 at 14:20

3 Answers 3

2

From the iPython console, you'd just do (assuming file is accessible in sys.path- this would include the CWD):

In [1]: import movie_analysis

That would then run your file, and output everything within that shell as if you had run it by double-click.

Sign up to request clarification or add additional context in comments.

Comments

2

If you're interested in running the script step-by-step, for example for debugging purposes, you can have a look at IPython Notebook. It's quite neat and interactive, you can divide your code in cells, then run the cells individually.

Another alternative of course would be to use an IDE with an actual debugger, for example Pydev, a Python IDE for eclipse. However, this might be a bit of an overkill in your situation, as it takes some time learning to use it.

If you're interested in running the entire script, I would suggest going with

$ python movie_analysis.py

as suggested by sshashank124, or import movie_analysis.

Comments

1

Why not just execute the script. You can do it as follows from the command-line (cmd for windows or sh for unix):

python movie_analysis.py

Or instead, if you want to use selected functions, methods and classes (assuming you don't have any script outside of a method that would get executed immediately), you can do:

import movie_analysis.py from the command line

1 Comment

import doesn't just make functions, methods, and classes available, it runs the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.