#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Python’s ‘import’ statement…


    Here’s my code in a file, seqslice.py :

    Code:
    s = input('Enter something: ')
    def seq(s):
                    while s:
                                    s = list(s)
                                    print(''.join(s))
                                    del s[-1]
    seq(s)
    In the interactive prompt, when I type ‘import seqslice’ it executes the way it should. But, when I again type ‘import seqslice’ it doesn’t run my script and simply returns to the standard interactive prompt’s ‘>>> ’
    Why doesn’t it run my source code again the way it did the first time?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Python modules are only imported once. If you want to use it again, you have to reload using the 'imp' module.

    Based on your print statements, I'm assuming you use python3.

    Code:
    >>> import seq
    Enter something: 1 1
    1 1
    1 
    1
    >>> import imp
    >>> imp.reload(seq)
    Enter something: 1 2 3
    1 2 3
    1 2 
    1 2
    1 
    1
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by partoj
    Python modules are only imported once. If you want to use it again, you have to reload using the 'imp' module.

    Based on your print statements, I'm assuming you use python3.

    Code:
    >>> import seq
    Enter something: 1 1
    1 1
    1 
    1
    >>> import imp
    >>> imp.reload(seq)
    Enter something: 1 2 3
    1 2 3
    1 2 
    1 2
    1 
    1
    OK,thanks a lot. And yeah, I use Python 3.0

IMN logo majestic logo threadwatch logo seochat tools logo