Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I understand what you're asking now I think. If you want to execute something in a file from the shell, the easiest way is to encapsulate it in a Class.</p> <p>Try changing your code to the following:</p> <pre><code>#!/usr/bin/python class Hello: def __init__(self): print "Hello, Interactive Shell World!" raw_input() </code></pre> <p>This makes a class called Hello where the constructor function runs the code you have in your current file. Then, start an interactive shell started in the same directory as the hello.py file. Here is a paste from an example session:</p> <pre><code>&gt;&gt;&gt; from hello import Hello &gt;&gt;&gt; Hello() Hello, Interactive Shell World! &lt;hello.Hello instance at 0xb782686c&gt; &gt;&gt;&gt; </code></pre> <p>It prints the message, waits for input, then prints the string representation of the newly created object and returns to the prompt. If you want to avoid the last printout just assign the object to a variable like:</p> <pre><code>&gt;&gt;&gt; h = Hello() </code></pre> <p>If you want to keep the ability to execute the file from the command line (rather than the shell) add this code to the bottom of the file:</p> <pre><code>if __name__ == '__main__': Hello() </code></pre> <p>You do "chmod +x" from the terminal. In the directory of the hello.py, run:</p> <p>chmod +x hello.py</p> <p>This gives you the ability to run your file like ./hello.py instead of "python hello.py". Now that I think of it, it sounds like you may be confusing the python interactive shell which a unix shell. You can run your file easily using "python hello.py" from a unix shell, but to run code from the python interactive shell, you will want to do something like I did above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload