Python installation: เรื่อง (ที่อยากโม้) เกี่ยวกับ path ของ data file

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
from distutils.core import setup
 
setup(name='Yatha parser',
      version='0.0.1',
      description='Thai PCFG Parser based on NLTK-Lite',
      author='Vee Satayamas',
      author_email='vsatayamas@gmail.com',
      url='http://naist.cpe.ku.ac.th/',
      packages=['yatha_parser'],
      package_dir={'yatha_parser': '.'},
      package_data={'yatha_parser': ['pcfg.txt']})

ใน python เราทำ installation script ได้ง่ายโดยการเขียนไฟล์แบบข้่างบนลงใน setup.py แล้วเราก็จะทำอะไรแบบ python setup.py install ได้ (เหมือนโปรแกรมของชาวบ้าน)

แต่ว่ามันก็จะมีปัญหาอยู่นิดหน่อยอย่างไฟล์ pcfg.txt (ที่ระบุในบรรทัดที่ 11) เป็น data file ที่ script จะ install ให้และ เราต้องไปสั่งเปิดจาก python code ของเรา ถ้าเปิดดื้อแบบข้างล่างก็เจ๊งครับ

(open("pcfg.txt")).read()

ต้องใส่มีท่าหา path ให้มันหน่อย แบบ code ด้านล่างนี่ก็เป็นอันใช้ได้ครับ

(open(os.path.join(os.path.dirname(__file__), "pcfg.txt")).read()

ปล. รอ mediawiki อยู่นะ lol

sugree's picture

ถามเล่นๆ Yatha อ่านว่าอะไร

veer's picture

yatha parser -> yet another thai parser :-P

sugree's picture

เด็กยุคใหม่ใช้ setuptools จะได้ไข่เอาไปฟักฟรีๆ

from setuptools import setup

และอย่าลืม MANIFEST.in

include pcfg.txt

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น