Python newbie help (please read)

Discussion in 'Mac Programming' started by Dmac77, Jan 28, 2009.

  1. macrumors 68020

    Dmac77

    #1
    Hi all,

    In the book I'm currently using, I'm working on Module imports. I'm trying to import a module but I'm stuck. The book (Learning Python) has its code written for a DOS window, instead of the terminal, so please forgive me for quoting the DOS commands out of the book, instead of UNIX commands.

    So I'm trying to import a script that i wrote earlier that's called "script4". The book says to do the following:

    Code:
    D:\LP3E\Examples> c:\python25\python
    >>> import script4
    win32
    [I]A long string of numbers shows up here[/I]
    So I went and started up python in the terminal, and told it to import script4:
    Code:
    Last login: Thu Jan 29 02:27:07 on ttys000
    name of computer:~ homefolder$ python
    Python 2.5.1 (r251:54863, Nov 11 2008, 17:46:48) 
    [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import script4
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named script4
    >>> 
    And that is what I get.

    I'm totally lost here. If it matters, the script is located at:

    Code:
    ~/Developer/script4.py
    Thanks!

    Don
     
  2. macrumors 68030

    Catfish_Man

    #2
    Try doing cd Developer first so you're in the same directory as the file you're importing.
     
  3. macrumors 6502a

    #3
    So when you import a module, the python interpreter will look for that code in a particular set of directories.

    If you want to know where it will look just enter:

    > import sys
    > sys.path

    This will show you all the directories where the interpreter will search for your module.

    By default, this search path will include your current directory.

    So as Catfish_Man said, the easiest way is to be in the directory when you start the interpreter.

    Hope that helps.
     
  4. macrumors 68020

    Dmac77

    #4
    Sorry that I haven't posted until now. But I was out all day. I followed your guys' advise, and it worked:D Thanks! I was getting really frustrated last night.

    Don
     

Share This Page