0

I wrote following python program

#! /usr/bin/python
def checkIndex(key):
    if not isinstance(key, (int, long)): raise TypeError
    if key<0: raise IndexError

class ArithmeticSequence:
    def __init__(self, start=0, step=1):
        self.start = start      # Store the start value
        self.step = step        # Store the step value
        self.changed = {}       # No items have been modified
    def __getitem__(self, key):
        checkIndex(key)
        try: return self.changed[key]
        except KeyError:
            return self.start + key*self.step
    def __setitem__(self, key, value):
        checkIndex(key)
        self.changed[key] = value

the program is my.py when I do

chmod +x my.py
python my.py

I am back to bash shell here after this step I open a python shell

user@ubuntu:~/python/$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> s=ArithmeticSequence(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ArithmeticSequence' is not defined

How do I give input to my program and run it because it was saved in vi

6
  • 3
    docs.python.org/2/tutorial/modules.html Commented Jun 5, 2013 at 18:28
  • 2
    You have to import the module first: >>> import my; s = my.ArithmeticSequence(1,2). Commented Jun 5, 2013 at 18:30
  • does the location of my program matters to import the module Commented Jun 5, 2013 at 18:34
  • I got import error import my.py Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named py Commented Jun 5, 2013 at 18:36
  • 1
    @RegisteredUser Yes, the location matters. You have to start the interpreter in the same directory where you have the script my.py. Commented Jun 5, 2013 at 18:37

3 Answers 3

1

Put your file my.py in PYTHONPATH then

from my import ArithmeticSequence
s=ArithmeticSequence(1,2)
Sign up to request clarification or add additional context in comments.

Comments

0

The command that you want to run is:

python -i my.py

That will parse my.py and define the name ArithmeticSequence, and drop you into the Python shell where you can use your objects interactively:

>>> s=ArithmeticSequence(1,2)
>>> 

7 Comments

your method python - scriptname.py does work , I used as some one suggested >>> import my and that also worked well, how ever I wrote another program then $bash]python -i some.py works but doing with >>>import some and then running the interactive shell does not work here is the program pastebin.com/deQszXGJ and to run in interactive shell I just do r=Rectangle() in shell try with your method and method as some one else suggests
In case you know the reason as why the other method did not work let me know
You forgot to add #! to the beginning of that file on pastebin. Here's the fix: pastebin.com/6HqJcB5n
hmmm thanks for the fix but I tried the program and the other thing of imporiting as >>>import some does not work and I get error when I do a r=Rectangle() but the same thing (r=Rectangle) does not give an error when I import by python -i some
If using import some then you have to use r=some.Rectangle().
|
0

Well you either have to run this as a program using

if __name__ == 'main':
    # Your code goes here. This will run when called from command line.

or if you are in the python interpreter you have to import "my" with:

>>> import my

Comments

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.