[triangle-zpug] can't find font problem

David Handy david at handysoftware.com
Sun Mar 30 03:51:19 UTC 2008


The Python path is only used for finding Python modules and packages, and
then only by the "import" statement. For opening regular files, you have to
specify a full pathname or a path relative to the current directory.

In this case, it looks you are trying to load the font file out of the PIL
package directory. You can find the directory of a Python package like this:

>>> import PIL
>>> PIL.__file__
'/usr/lib/python2.4/site-packages/PIL/__init__.pyc'
>>> import os
>>> os.path.dirname(PIL.__file__)
'/usr/lib/python2.4/site-packages/PIL'

So I suppose you could do something like this in your code:

label_font=ImageFont.load(os.path.dirname(PIL.__file__) + "/courR18.pil")

By the way, I just now looked in the PIL directory on my computer and didn't
see any .pil files there; I assume you put courR18.pil in that directory
yourself?

Hope this helps -
David H

On Sat, Mar 29, 2008 at 06:42:01PM -0700, Joseph Mack NA3T wrote:
> I have a font which I can find with the full pathname
> 
> #-----code starts here
> from math import sqrt
> import os, sys
> from PIL import Image, ImageDraw, ImageFont
> 
> 
> print sys.path
> #fonts
> #THIS LINE WORKS
> #label_font=ImageFont.load("/usr/lib/python2.4/site-packages/PIL/courR18.pil")
> #THIS LINE FAILS
> label_font=ImageFont.load("PIL/courR18.pil")
> #-------code ends here
> 
> 
> I get an error with any variation of the font filename where 
> I left python handle the python path, including just the 
> bare filename.
> 
> Here's python path from the code
> 
> ['/src/da/python_class/class_code',
> '/usr/lib/python24.zip', 
> '/usr/lib/python2.4',
> '/usr/lib/python2.4/plat-linux2', 
> '/usr/lib/python2.4/lib-tk', 
> '/usr/lib/python2.4/lib-dynload', 
> '/usr/lib/python2.4/site-packages', 
> '/usr/lib/python2.4/site-packages/PIL']
> 
> the error is
> 
> Traceback (most recent call last):
>    File "./pi_numerical_integration_diagram.py", line 55, in ?
>      label_font = ImageFont.load("PIL/courR18.pil")
>    File "/usr/lib/python2.4/site-packages/PIL/ImageFont.py", 
> line 180, in load
>      f._load_pilfont(filename)
>    File "/usr/lib/python2.4/site-packages/PIL/ImageFont.py", 
> line 67, in _load_pilfont
>      file = open(filename, "rb")
> IOError: [Errno 2] No such file or directory: 
> 'PIL/courR18.pil'
> 
> 
> I've got code that runs, but I'd like to get my python 
> path working. What am I doing wrong?
> 
> Thanks Joe
> 
> -- 
> Joseph Mack NA3T EME(B,D), FM05lw North Carolina
> jmack (at) wm7d (dot) net - azimuthal equidistant map
> generator at http://www.wm7d.net/azproj.shtml
> Homepage http://www.austintek.com/ It's GNU/Linux!
> 
> _______________________________________________
> triangle-zpug mailing list
> triangle-zpug at starship.python.net
> http://starship.python.net/mailman/listinfo/triangle-zpug
> 

-- 
David Handy
Computer Programming is Fun!
Beginning Computer Programming with Python
http://www.handysoftware.com/cpif/



More information about the triangle-zpug mailing list