The Famous Error Message: "ImportError: No module named python_script"

Discussion in 'Python' started by rich murphy, Dec 14, 2006.

  1. rich murphy

    rich murphy Guest

    I am studying Python language. I have Python 2.5 installed in my PC
    which is running on Windows XP. I placed the the script called
    "python_script" in C:\Python25 directory where all the other Python
    files are.

    When I tried to import the script called "python_script", it kept
    printing the famous error message: "ImportError: No module named
    python_script".

    I took the file out of "C:\Python25" directory, placed it in
    'C:\PythonTests'. The error message kept coming.

    The "import" commnand will not work at all.


    >>> import python_script


    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named python_script

    >>> import sys


    >>> sys.path

    ['', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs',
    'C:\\Python25\
    \lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk',
    'C:\\Python25
    ', 'C:\\Python25\\lib\\site-packages']

    >>> sys.path

    ['', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs',
    'C:\\Python25\
    \lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk',
    'C:\\Python25
    ', 'C:\\Python25\\lib\\site-packages']

    >>> sys.path.append('C:\PythonTests')


    >>> sys.path

    ['', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs',
    'C:\\Python25\
    \lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk',
    'C:\\Python25
    ', 'C:\\Python25\\lib\\site-packages', 'C:\\PythonTests']


    I uninstall Python2.5, installed Python2.4: the result is the same. The
    result is the same with older versions of Python also.

    Does anybody know a remedy for this???
     
    rich murphy, Dec 14, 2006
    #1
    1. Advertising

  2. Re: The Famous Error Message: "ImportError: No module namedpython_script"

    At Wednesday 13/12/2006 22:16, rich murphy wrote:

    >I am studying Python language. I have Python 2.5 installed in my PC
    >which is running on Windows XP. I placed the the script called
    >"python_script" in C:\Python25 directory where all the other Python
    >files are.


    Verify the file name, should be "python_script.py"

    You may want to un-select "Hide extensions for known file types" in
    Windows Explorer options.


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
     
    Gabriel Genellina, Dec 14, 2006
    #2
    1. Advertising

  3. rich murphy

    Ben Finney Guest

    Re: The Famous Error Message: "ImportError: No module namedpython_script"

    "rich murphy" <> writes:

    > I am studying Python language.


    Welcome! Allow me to direct you to the Python tutorial:

    <URL:http://docs.python.org/tut/>

    Please take the time to work through all the exercises in that
    document, understanding each one before moving on.

    I recommend this because:

    > I placed the the script called "python_script" in C:\Python25
    > directory where all the other Python files are.


    you would not make this mistake if you had already worked through the
    tutorial.

    Enjoy!

    --
    \ "On the other hand, you have different fingers." -- Steven |
    `\ Wright |
    _o__) |
    Ben Finney
     
    Ben Finney, Dec 14, 2006
    #3
  4. rich murphy

    rich murphy Guest

    Thank you both for responding.

    Yes of course the file has the ".py" extension and yes I went through
    the tutorial.


    Gabriel Genellina wrote:
    > At Wednesday 13/12/2006 22:16, rich murphy wrote:
    >
    > >I am studying Python language. I have Python 2.5 installed in my PC
    > >which is running on Windows XP. I placed the the script called
    > >"python_script" in C:\Python25 directory where all the other Python
    > >files are.

    >
    > Verify the file name, should be "python_script.py"
    >
    > You may want to un-select "Hide extensions for known file types" in
    > Windows Explorer options.
    >
    >
    > --
    > Gabriel Genellina
    > Softlab SRL
    >
    > __________________________________________________
    > Correo Yahoo!
    > Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    > ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
     
    rich murphy, Dec 14, 2006
    #4
  5. Re: The Famous Error Message: "ImportError: No module namedpython_script"

    rich murphy wrote:

    > Thank you both for responding.
    >
    > Yes of course the file has the ".py" extension and yes I went through
    > the tutorial.


    since everyone on this forum is importing modules successfully hundreds
    of times every day, that's not obvious at all.

    try running the interpreter as

    python -vv

    and see what it prints when you type

    import python_script

    </F>
     
    Fredrik Lundh, Dec 14, 2006
    #5
  6. rich murphy

    rich murphy Guest

    Ben Finney wrote:
    > "rich murphy" <> writes:
    >
    > > I am studying Python language.

    >
    > Welcome! Allow me to direct you to the Python tutorial:
    >
    > <URL:http://docs.python.org/tut/>
    >
    > Please take the time to work through all the exercises in that
    > document, understanding each one before moving on.
    >
    > I recommend this because:
    >
    > > I placed the the script called "python_script" in C:\Python25
    > > directory where all the other Python files are.

    >
    > you would not make this mistake if you had already worked through the
    > tutorial.


    The tutorial says: "For instance, use your favorite text editor to
    create a file called fibo.py in the current directory with the
    following contents:"

    So, I assumed "the current directory" is C:\Python25 which did not
    work. Then I placed the fibo.py file in C: director. That did not work
    either. What directory does it mean then?


    >
    > Enjoy!
    >
    > --
    > \ "On the other hand, you have different fingers." -- Steven |
    > `\ Wright |
    > _o__) |
    > Ben Finney
     
    rich murphy, Dec 14, 2006
    #6
  7. Re: The Famous Error Message: "ImportError: No modulenamed python_script"

    rich murphy wrote:

    >So, I assumed "the current directory" is C:\Python25 which did not
    >work. Then I placed the fibo.py file in C: director. That did not work
    >either. What directory does it mean then?
    >

    OK, forgive me for using 2.4... Can you import "sys"? Assuming you've
    got python_script.py at path: "C:\\python_script.py" you might try this
    quick test:

    >>> import sys
    >>> sys.path

    ['C:\\Python24\\Lib\\idlelib', 'C:\\windows\\system32\\python24.zip',
    'C:\\Python24', 'C:\\Python24\\DLLs', 'C:\\Python24\\lib',
    'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk',
    'C:\\Python24\\lib\\site-packages',
    'C:\\Python24\\lib\\site-packages\\win32',
    'C:\\Python24\\lib\\site-packages\\win32\\lib'] ## your ouput may be
    different
    >>> sys.path.append("C:\\")
    >>> sys.path

    ['C:\\Python24\\Lib\\idlelib', 'C:\\windows\\system32\\python24.zip',
    'C:\\Python24', 'C:\\Python24\\DLLs', 'C:\\Python24\\lib',
    'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk',
    'C:\\Python24\\lib\\site-packages',
    'C:\\Python24\\lib\\site-packages\\win32',
    'C:\\Python24\\lib\\site-packages\\win32\\lib', 'C:\\']
    >>> import python_script



    6.1.1 The Module Search Path

    When a module named spam is imported, the interpreter searches for a
    file named spam.py in the current directory [...]

    Actually, modules are searched in the list of directories given by the
    variable |sys.path| which is initialized from the directory containing
    the input script (or the current directory), PYTHONPATH and the
    installation-dependent default. This allows Python programs that know
    what they're doing to modify or replace the module search path. Note
    that because the directory containing the script being run is on the
    search path, it is important that the script not have the same name as a
    standard module, or Python will attempt to load the script as a module
    when that module is imported. This will generally be an error. See
    section 6.2 <#standardModules>, ``Standard Modules,'' for more information.
     
    Eric Pederson, Dec 14, 2006
    #7
  8. Re: The Famous Error Message: "ImportError: No module namedpython_script"

    At Thursday 14/12/2006 03:44, rich murphy wrote:

    >The tutorial says: "For instance, use your favorite text editor to
    >create a file called fibo.py in the current directory with the
    >following contents:"
    >
    >So, I assumed "the current directory" is C:\Python25 which did not
    >work. Then I placed the fibo.py file in C: director. That did not work
    >either. What directory does it mean then?


    It doesn't matter *which* directory you are, as far as you don't
    change it when you enter the interpreter:

    C:\TEMP\test\basura>type fibo.py
    print "inside",__file__

    C:\TEMP\test\basura>python
    Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import fibo

    inside fibo.py
    >>>


    When it later says "enter the Python interpreter" that means "execute
    python from the command line", not PythonWin nor IDLE nor...


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
     
    Gabriel Genellina, Dec 14, 2006
    #8
  9. rich murphy

    rich murphy Guest

    Thanks to everyone who responded with valuable suggestions. I
    appreciate them all. I found the exact reason why "import" command
    fails.

    At the beginning I created my script file with MS Notepad. After
    studying all the advice, I realized that I did not try other text
    editors just becuase the tutorial says: "use your favorite text editor
    to create a file called fibo.py in the current directory with the
    following contents:"

    Then I created another file with the same stuff in it using WordPad.
    The behaviour of the "import" command improved a lot but still not
    good. Then I used MS-DOS' text editor to create the same file.
    Immediately the "import" command worked as it was supposed to. Almost.
    "fibo.fib2(100)" command stil does not work.


    This is fine:
    >>> fibo.fib(1000)

    1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

    This not fine:
    >>> fibo.fib2(100)

    [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]


    When I enter "fibo.fib2(100)", I get the following:

    >>> fibo.fib2(100)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "C:\Python24\fibo.py", line 11, in fib2
    while b < n:
    UnboundLocalError: local variable 'b' referenced before assignment


    This is fine:
    >>> fibo.__name__

    'fibo'
    >>> fib=fibo.fib
    >>> fib(500)

    1 1 2 3 5 8 13 21 34 55 89 144 233 377

    Both Python2.4 and 2.5 are behaving the same. I will also try to create
    the file with Vi editor.
     
    rich murphy, Dec 14, 2006
    #9
  10. rich murphy schreef:
    > This not fine:
    >>>> fibo.fib2(100)

    > [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    >
    >
    > When I enter "fibo.fib2(100)", I get the following:
    >
    >>>> fibo.fib2(100)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "C:\Python24\fibo.py", line 11, in fib2
    > while b < n:
    > UnboundLocalError: local variable 'b' referenced before assignment


    Can you show the code of fib2? It looks like there's an error in it, but
    without the code it's hard to say anything about it.

    > Both Python2.4 and 2.5 are behaving the same. I will also try to create
    > the file with Vi editor.


    If by Vi you mean Vim, that's my favorite editor for editing Python
    code, with Idle second.

    --
    If I have been able to see further, it was only because I stood
    on the shoulders of giants. -- Isaac Newton

    Roel Schroeven
     
    Roel Schroeven, Dec 14, 2006
    #10
  11. Re: The Famous Error Message: "ImportError: No module namedpython_script"

    At Thursday 14/12/2006 17:13, rich murphy wrote:

    >Thanks to everyone who responded with valuable suggestions. I
    >appreciate them all. I found the exact reason why "import" command
    >fails.
    >
    >At the beginning I created my script file with MS Notepad. After
    >studying all the advice, I realized that I did not try other text
    >editors just becuase the tutorial says: "use your favorite text editor
    >to create a file called fibo.py in the current directory with the
    >following contents:"


    I bet your file is actually called fibo.py.txt then.
    (Perhaps some advise should be provided for Windows users, about
    enclosing the name inside "double quotes", or selecting "All files
    (*.*)" on the file type list)

    >Then I created another file with the same stuff in it using WordPad.
    >The behaviour of the "import" command improved a lot but still not
    >good.


    Uhm... either the import was successful, or not. Care to explain what
    you mean?

    > >>> fibo.fib2(100)

    >Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "C:\Python24\fibo.py", line 11, in fib2
    > while b < n:
    >UnboundLocalError: local variable 'b' referenced before assignment


    Compare your function with the version shown in the tutorial.
    Variable 'b' is assigned on the previous line.


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
     
    Gabriel Genellina, Dec 15, 2006
    #11
  12. rich murphy

    greg Guest

    rich murphy wrote:
    > So, I assumed "the current directory" is C:\Python25 which did not
    > work... What directory does it mean then?


    It means the current directory of the process running
    the Python interpreter, which, unless you've done
    something to change it, will be the same as the
    current directory of the command shell that you
    ran the Python interpreter from. That, in turn,
    will be whatever you set it to using the "cd" command.

    However, it's important to understand that this only
    applies when using the Python interpreter *interactively*,
    which means just typing "python" and entering Python
    statements at the >>> prompt.

    If you tell the interpreter to run a .py file,
    e.g.

    python some\dir\myfile.py

    then it does *not* look in the current directory of
    the process for imported files -- instead, it looks in the
    directory containing the "main" .py file, i.e. the one
    you passed on the command line. This is more useful,
    since it allows you to keep the main file and the
    modules it uses together, and not have to worry about
    cd'ing to the directory in order to run it.

    Also, if you're using an IDE such as IDLE or PythonWin
    instead of running the interpreter from a command shell,
    relying on the "current directory" isn't very useful,
    since it's hard to predict what it will be. Again,
    just put the files to be imported alongside your main
    ..py file and you should be all right.

    There are various ways of specifying additional places
    to look for imported modules, but that should be enough
    to get you going.

    --
    Greg
     
    greg, Dec 15, 2006
    #12
  13. rich murphy

    greg Guest

    Re: The Famous Error Message: "ImportError: No module named python_script"

    Gabriel Genellina wrote:

    > I bet your file is actually called fibo.py.txt then.
    > (Perhaps some advise should be provided for Windows users, about
    > enclosing the name inside "double quotes", or selecting "All files
    > (*.*)" on the file type list)


    Also, if by some chance you've got "Hide filename
    extensions" turned on in your View options, TURN
    IT OFF. It'll cause no end of confusion when you're
    trying to program.

    --
    Greg
     
    greg, Dec 15, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loser
    Replies:
    3
    Views:
    1,340
    loser
    Aug 7, 2003
  2. Famous XML cliches

    , Aug 22, 2005, in forum: XML
    Replies:
    16
    Views:
    795
    Nick Kew
    Aug 29, 2005
  3. Lew
    Replies:
    7
    Views:
    1,056
    Mike Schilling
    Mar 23, 2009
  4. loser
    Replies:
    0
    Views:
    126
    loser
    Aug 7, 2003
  5. loser
    Replies:
    0
    Views:
    120
    loser
    Aug 7, 2003
Loading...

Share This Page