[Pythonmac-SIG] More newbie questions

Bob Ippolito bob at redivi.com
Sun Aug 29 02:32:42 CEST 2004


On Aug 28, 2004, at 8:21 PM, Calvin wrote:

>> This next one is a bit more of an issue, though. How can I get
>> imports to work from the command line in IDLE or PythonIDE? I can't
>> seem to figure out any way to set the current working directory.
>> Trying to put the full path on the command line simply results in an
>> error. This might be simple, but it is stumping me and the limited
>> documentation doesn't seem to help. Do I have to set the sys.path to
>> include my scripts directory (/Users/rodneys/Documents/python)? If
>> so, how do I go about this?
>
> My cheap-o answer to this problem is either to use terminal and start
> python from the directory where your objects are (my prefered method)  
> or
>
> import os
> and os.chdir('your path')
>
> then you can import the objects in that directory.  Of course, I did  
> this
> from terminal so your mileage may vary.

A better solution is to just use  
/Users/rodneys/Library/Python/2.3/site-packages as your scripts  
directory

or do this to add your python documents directory to your sys.path  
(just do this once):

import os
try:
      
os.makedirs(os.path.expanduser('~/Library/Python/2.3/site-packages'))
except:
     pass
file(os.path.expanduser('~/Library/Python/2.3/site-packages/myscripts. 
pth'), 'w').write(os.path.expanduser('~Documents/python'))

-bob


More information about the Pythonmac-SIG mailing list