python module import problems

I have a python module called HTMLTableParse. The directory containing the module is set in PYTHONPATH in ~/.MacOSX/environment.plist. It imports fine when I am running python from the command line or using IDLE.app. However, when I try to run a python module from a cron job, I get this error:

ImportError: No module named HTMLTableParse


So then I tried setting PYTHONPATH in my crontab. When I did that, it successfully imported HTMLTableParse, but failed on a standard python module:

File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/shelve. py", line 231, in open
return DbfilenameShelf(filename, flag, protocol, writeback, binary)
File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/shelve. py", line 212, in _init_
Shelf. _init_(self, anydbm.open(filename, flag), protocol, writeback, binary)
File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/anydbm. py", line 82, in open
mod = _import_(result)
File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/dbhash. py", line 5, in ?
import bsddb
File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/bsddb/ _init_.py", line 40, in ?
import _bsddb
ImportError: No module named _bsddb
Exception exceptions.AttributeError: "DbfilenameShelf instance has no attribute 'writeback'" in ignored


The bdssb module appears to be in /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5, so I added that path to PYTHONPATH in my crontab. Then I got:

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 97, in ?
import sre_compile
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sre_compile.py ", line 17, in ?
assert _sre.MAGIC == MAGIC, "SRE module mismatch"
AssertionError: SRE module mismatch


What's going on? What do I need to do to run a python script from cron?

Also, how do I format code in my posts in this forum? I've seen other users do it, but the user help file doesn't describe how to do it. I've settled for using ">", which doesn't look great.

Thanks in advance. I'm desperate to get this to work.

Mac Mini Intel Core Duo, Mac OS X (10.4.4)

Posted on Nov 10, 2007 5:32 PM

Reply
2 replies

Nov 10, 2007 7:28 PM in response to capmac

I guess you are using Tiger.

Python2.3 is installed by Apple in /usr/bin/python, and it uses /System/Library/Frameforks/Python.framework/Version/2.3/ (and /Library/Python/2.3/).

If you have installed Python2.5 by yourself, it is usually installed as /usr/local/bin/python, and uses /Library/Frameworks/Python.framework/Version/2.5/.

I believe you have added /usr/local/bin to the front of PATH in your .bash_profile (or .bashrc or .zshrc or ...). So if you start python from your command line, /usr/local/bin/python (=Python2.5) is used.

But in your cron job, PATH does not include /usr/local/bin, and /usr/bin/python (Phthon2.3) is used.

If you want to use Python2.5 in your cron script, just set PATH in the script

PATH=/usr/local/bin:$PATH

or explicitly invoke /usr/local/bin/python.

how do I format code in my posts in this forum?


This is "undocumented" (why ?). You can use


your code here

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

python module import problems

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.