Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 29, 2009, 01:43 AM   #1
Dmac77
macrumors 68020
 
Dmac77's Avatar
 
Join Date: Jan 2008
Location: Michigan
Python newbie help (please read)

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
A long string of numbers shows up here
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

Last edited by Dmac77; Feb 23, 2009 at 03:46 PM.
Dmac77 is offline   0 Reply With Quote
Old Jan 29, 2009, 02:28 AM   #2
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Try doing cd Developer first so you're in the same directory as the file you're importing.
Catfish_Man is offline   0 Reply With Quote
Old Jan 29, 2009, 11:22 AM   #3
eddietr
macrumors 6502a
 
Join Date: Oct 2006
Location: Virginia
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.
eddietr is offline   0 Reply With Quote
Old Jan 30, 2009, 01:32 AM   #4
Dmac77
Thread Starter
macrumors 68020
 
Dmac77's Avatar
 
Join Date: Jan 2008
Location: Michigan
Sorry that I haven't posted until now. But I was out all day. I followed your guys' advise, and it worked Thanks! I was getting really frustrated last night.

Don
Dmac77 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
G4 optical drive will not read DVD's but will read CD's TheMissingLink5 Mac Basics and Help 2 Jul 30, 2013 03:31 PM
Lost my read access to read NTFS drives bugsy123 MacBook Pro 2 Jul 20, 2013 10:46 AM
Python 3.2.3 dboxall123 Mac Programming 4 Sep 27, 2012 10:53 AM
Read mail on iOS not marked as read on mac. Toml2003 OS X 0 Sep 26, 2012 04:00 PM
Read mail on iOS not marked as read on mac. Toml2003 Mac Basics and Help 0 Sep 26, 2012 05:40 AM

Forum Jump

All times are GMT -5. The time now is 10:56 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC