Tôi gặp sự cố khi phân phối ứng dụng sử dụng pytz. Tôi đang sử dụng Py2Exe để tạo tệp thực thi từ nguồn Python của tôi.UnknownTimezoneError Ngoại lệ được nâng lên với ứng dụng Python được biên dịch bằng Py2Exe
Đối với một ví dụ đơn giản của vấn đề tôi đang gặp, tôi có: pytz_test.py:
import pytz
tz_au = pytz.timezone("Australia/Sydney")
print tz_au
và trong setup.py:
from distutils.core import setup
import py2exe
setup(console=['pytz_test.py'], options={"py2exe" : { 'packages': ['pytz'], } })
sau đó tôi chạy setup.py :
python setup.py py2exe
Việc biên dịch tệp thực thi. Chạy tạo pytz_test.exe tôi nhận được:
Tôi cho rằng đó là bởi vì thông tin múi giờ là không nhận được đi kèm với các thực thi, nhưng tôi không chắc chắn làm thế nào để làm cho nó xảy ra.
EDIT: Một giải pháp đơn giản sẽ có thêm thư mục zoneinfo, từ pytz mô-đun trong python thư mục trang web-bì, đến library.zip.
Để làm điều này tự động, Tôi làm theo các giải pháp trong dự án mà Google Transit liệu thức ăn sử dụng, từ: http://code.google.com/p/googletransitdatafeed/source/browse/trunk/python/setup.py
My sửa đổi setup.py bây giờ trông giống như:
from distutils.core import setup
import glob
import py2exe
options = {
"py2exe" : {
"compressed": 1,
"optimize": 2,
'packages': ['pytz'],
}
}
setup(console=['pytz_test.py'], options=options)
import pytz
import os
import zipfile
zipfile_path = os.path.join("dist/" 'library.zip')
z = zipfile.ZipFile(zipfile_path, 'a')
zoneinfo_dir = os.path.join(os.path.dirname(pytz.__file__), 'zoneinfo')
disk_basedir = os.path.dirname(os.path.dirname(pytz.__file__))
for absdir, directories, filenames in os.walk(zoneinfo_dir):
assert absdir.startswith(disk_basedir), (absdir, disk_basedir)
zip_dir = absdir[len(disk_basedir):]
for f in filenames:
z.write(os.path.join(absdir, f), os.path.join(zip_dir, f))
z.close()
Bạn cũng cần phiên bản hiện tại của công cụ thiết lập. Cụ thể pkg_resources. 'nhập pkg_resources' không được thất bại. – sbaechler
Trường hợp sử dụng 'pkg_resources' chính xác ở đâu? hoặc chỉ bằng cách nhập nó? – Triztian